How to tell if Angular 6 App is in production mode outside of Angular project












0















I have some Google Analytics code that is outside of the Angular project. I only want to include it if the environment is production.



I know there is an environment variable inside the Angular project that you can reference like this inside typescript...



if (environment.production) {
// do stuff
}


That doesn't help if there is some static code outside of Angular though. I tried putting the Google analytics code inside Angular, but that seems to have issues with the scope not being found and typescript didn't like it.



Right now the best solution I have is to manually check the hostname which probably isn't the best solution (this is in the index.html outside the angular app)...



<script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-0"></script>
<script>
if(location.hostname === "productionurl.com") {
// insert google analytics tracking code
window.dataLayer = window.dataLayer || ;
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-000000000-0');
}
</script>


I am used to using web.config files which would be simple, but this project uses an angular config instead to deal with environment variables.



Maybe there is a way to get the environment from something like "getAllAngularRootElements()" in vanilla javascript, but I haven't found a way.










share|improve this question





























    0















    I have some Google Analytics code that is outside of the Angular project. I only want to include it if the environment is production.



    I know there is an environment variable inside the Angular project that you can reference like this inside typescript...



    if (environment.production) {
    // do stuff
    }


    That doesn't help if there is some static code outside of Angular though. I tried putting the Google analytics code inside Angular, but that seems to have issues with the scope not being found and typescript didn't like it.



    Right now the best solution I have is to manually check the hostname which probably isn't the best solution (this is in the index.html outside the angular app)...



    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-0"></script>
    <script>
    if(location.hostname === "productionurl.com") {
    // insert google analytics tracking code
    window.dataLayer = window.dataLayer || ;
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'UA-000000000-0');
    }
    </script>


    I am used to using web.config files which would be simple, but this project uses an angular config instead to deal with environment variables.



    Maybe there is a way to get the environment from something like "getAllAngularRootElements()" in vanilla javascript, but I haven't found a way.










    share|improve this question



























      0












      0








      0








      I have some Google Analytics code that is outside of the Angular project. I only want to include it if the environment is production.



      I know there is an environment variable inside the Angular project that you can reference like this inside typescript...



      if (environment.production) {
      // do stuff
      }


      That doesn't help if there is some static code outside of Angular though. I tried putting the Google analytics code inside Angular, but that seems to have issues with the scope not being found and typescript didn't like it.



      Right now the best solution I have is to manually check the hostname which probably isn't the best solution (this is in the index.html outside the angular app)...



      <script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-0"></script>
      <script>
      if(location.hostname === "productionurl.com") {
      // insert google analytics tracking code
      window.dataLayer = window.dataLayer || ;
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-000000000-0');
      }
      </script>


      I am used to using web.config files which would be simple, but this project uses an angular config instead to deal with environment variables.



      Maybe there is a way to get the environment from something like "getAllAngularRootElements()" in vanilla javascript, but I haven't found a way.










      share|improve this question
















      I have some Google Analytics code that is outside of the Angular project. I only want to include it if the environment is production.



      I know there is an environment variable inside the Angular project that you can reference like this inside typescript...



      if (environment.production) {
      // do stuff
      }


      That doesn't help if there is some static code outside of Angular though. I tried putting the Google analytics code inside Angular, but that seems to have issues with the scope not being found and typescript didn't like it.



      Right now the best solution I have is to manually check the hostname which probably isn't the best solution (this is in the index.html outside the angular app)...



      <script async src="https://www.googletagmanager.com/gtag/js?id=UA-00000000-0"></script>
      <script>
      if(location.hostname === "productionurl.com") {
      // insert google analytics tracking code
      window.dataLayer = window.dataLayer || ;
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'UA-000000000-0');
      }
      </script>


      I am used to using web.config files which would be simple, but this project uses an angular config instead to deal with environment variables.



      Maybe there is a way to get the environment from something like "getAllAngularRootElements()" in vanilla javascript, but I haven't found a way.







      angular google-analytics






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 17:07







      scottpetrovic

















      asked Nov 19 '18 at 16:26









      scottpetrovicscottpetrovic

      336




      336
























          2 Answers
          2






          active

          oldest

          votes


















          1














          For me the most clean solution at the moment is to use a second index.html file, for example index.prod.html, and make it replace the original one during the build process by using the "fileReplacements" property in the "production" env config inside angular.json.



          Check here: https://github.com/angular/angular-cli/wiki/stories-application-environments






          share|improve this answer
























          • This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

            – scottpetrovic
            Dec 10 '18 at 17:05



















          0














          Did you try setting up Google Analytics through Angulartics (https://github.com/angulartics/angulartics)? That should resolve your problem I think.






          share|improve this answer
























          • I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

            – scottpetrovic
            Nov 20 '18 at 22:59











          • It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

            – IgorKol
            Nov 21 '18 at 23:27













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378889%2fhow-to-tell-if-angular-6-app-is-in-production-mode-outside-of-angular-project%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          For me the most clean solution at the moment is to use a second index.html file, for example index.prod.html, and make it replace the original one during the build process by using the "fileReplacements" property in the "production" env config inside angular.json.



          Check here: https://github.com/angular/angular-cli/wiki/stories-application-environments






          share|improve this answer
























          • This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

            – scottpetrovic
            Dec 10 '18 at 17:05
















          1














          For me the most clean solution at the moment is to use a second index.html file, for example index.prod.html, and make it replace the original one during the build process by using the "fileReplacements" property in the "production" env config inside angular.json.



          Check here: https://github.com/angular/angular-cli/wiki/stories-application-environments






          share|improve this answer
























          • This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

            – scottpetrovic
            Dec 10 '18 at 17:05














          1












          1








          1







          For me the most clean solution at the moment is to use a second index.html file, for example index.prod.html, and make it replace the original one during the build process by using the "fileReplacements" property in the "production" env config inside angular.json.



          Check here: https://github.com/angular/angular-cli/wiki/stories-application-environments






          share|improve this answer













          For me the most clean solution at the moment is to use a second index.html file, for example index.prod.html, and make it replace the original one during the build process by using the "fileReplacements" property in the "production" env config inside angular.json.



          Check here: https://github.com/angular/angular-cli/wiki/stories-application-environments







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 3 '18 at 11:05









          AndreAndre

          10316




          10316













          • This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

            – scottpetrovic
            Dec 10 '18 at 17:05



















          • This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

            – scottpetrovic
            Dec 10 '18 at 17:05

















          This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

          – scottpetrovic
          Dec 10 '18 at 17:05





          This is a good solution. I have seen the file replace examples for other script files and such, but I didn't even think to swap out entire html files. Thanks for the easy and clean solution

          – scottpetrovic
          Dec 10 '18 at 17:05













          0














          Did you try setting up Google Analytics through Angulartics (https://github.com/angulartics/angulartics)? That should resolve your problem I think.






          share|improve this answer
























          • I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

            – scottpetrovic
            Nov 20 '18 at 22:59











          • It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

            – IgorKol
            Nov 21 '18 at 23:27


















          0














          Did you try setting up Google Analytics through Angulartics (https://github.com/angulartics/angulartics)? That should resolve your problem I think.






          share|improve this answer
























          • I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

            – scottpetrovic
            Nov 20 '18 at 22:59











          • It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

            – IgorKol
            Nov 21 '18 at 23:27
















          0












          0








          0







          Did you try setting up Google Analytics through Angulartics (https://github.com/angulartics/angulartics)? That should resolve your problem I think.






          share|improve this answer













          Did you try setting up Google Analytics through Angulartics (https://github.com/angulartics/angulartics)? That should resolve your problem I think.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 '18 at 18:22









          IgorKolIgorKol

          856




          856













          • I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

            – scottpetrovic
            Nov 20 '18 at 22:59











          • It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

            – IgorKol
            Nov 21 '18 at 23:27





















          • I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

            – scottpetrovic
            Nov 20 '18 at 22:59











          • It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

            – IgorKol
            Nov 21 '18 at 23:27



















          I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

          – scottpetrovic
          Nov 20 '18 at 22:59





          I am using Angulartics2 github.com/angulartics/angulartics2/tree/master/src/lib/… The instructions on that page say to put the normal angular setup code right after the body...which is outside the angular app. When the page first loads it automatically registers a page hit. I can turn off the "startTracking" stuff later, but the original page enter still happens. I guess it isn't that big of a deal, but it seemed like it would be neat if you could pull environment variables from the angular object since it is used to control the web project.

          – scottpetrovic
          Nov 20 '18 at 22:59













          It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

          – IgorKol
          Nov 21 '18 at 23:27







          It actually says that you should put standard GTM code at the beginning of the <body>, and then to import { Angulartics2GoogleTagManager } from 'angulartics2/gtm';

          – IgorKol
          Nov 21 '18 at 23:27




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378889%2fhow-to-tell-if-angular-6-app-is-in-production-mode-outside-of-angular-project%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?