1

I have this angular project and then index.html is the main entry point of the application like any other angular project , it contains the links.

As you can see on the html below there is an interpolation which is the ${environment.code} that is coming from the environment config.

The value of google tag manager id should be the value of ${environment.code} which is different for dev, staging and prod.

The problem is environment.code is not being accesssible in the index.html (tried different ways)

is there a way we can modify the index.html or at least modify the google tag manager id value for different environments? Thanks,

enter image description here

#index.html code

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="Cache-Control" content="max-age=0, must-revalidate"/>
  <meta http-equiv="Pragma" content="no-cache"/>
  <meta http-equiv="Expires" content="0" />
  <title>m</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="sec-validation" content="d1343a3ae51-8f7b-4038-bfc8-c56565485456547a4d">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="preconnect" href="https://fonts.gstatic.com">  
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">  
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id=${environment.code}'+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer',environment.code);</script>
  <!-- End Google Tag Manager -->
</head>

<body class="mat-typography dx-viewport">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${environment.code}"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
  <app-root></app-root>
</body>
</html>

#main.ts code

import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

#environment code

export const environment = {
  production: false,
  appName: 'My App',
  code: 'AGRTSRS-GMSDS'
};
5
  • Does this answer your question? How to use environment variable in index.html for Angular 6 Commented Jan 27, 2023 at 13:22
  • no sir , all are outdated answers Commented Jan 27, 2023 at 13:26
  • cant add script in app.component.html and then pass the environment? Commented Jan 27, 2023 at 13:33
  • what do you mean by that Sir ? can you ellaborate or post as an answer ? Thanks/ Commented Jan 27, 2023 at 13:35
  • but it is the index.html that I am trying to change , how does it ties on the app.component.htm;? Commented Jan 27, 2023 at 13:51

1 Answer 1

4

It is not possible to use env files in index.html, but you can provide a separate index file for each environment:

"configurations": {
   "production": {
    "index": {"input": "src/prod.index.html", "output": "index.html"}
    },
   "staging": {
    "index": {"input": "src/staging.index.html", "output": "index.html"}
    }
}

You could write schematics to extract env data and patch index.html but it seems to be too complicated for the task.

Another way is to use multiple JSON configuration files, you can import these files into env file and also you can write post-build script that patches index.html using apporpriate JSON file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.