Code Monkey home page Code Monkey logo

Comments (8)

mokipedia avatar mokipedia commented on July 20, 2024 2

you could probably do it with a useFactory function instead of useValue
something like:

providers: [
    {provide: 'googleTagManagerId', useFactory: () => {
            if (window.location.href.toString().includes('gtagId')) {
            const vars = {};
            const parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, 
            function(m,key,value) {
               return vars[key] = value;
            });
            return vars['gtagId'] || 'SOME_SENSIBLE_DEFAULT_IF_POSSIBLE';
          }
     }  
  ],

from angular-google-tag-manager.

mzuccaroli avatar mzuccaroli commented on July 20, 2024

I think that the source of the problem could be the usage of "window.location.href", global variables are usually messy
have you tried using the angular ActivatedRoute?

from angular-google-tag-manager.

joncorrin avatar joncorrin commented on July 20, 2024

This is in my app.module file so Im unsure if I can access ActivatedRoute at this point. Is that possible and or smart to do?

Also, its still logging the correct gtagId. So the variable should be fine

from angular-google-tag-manager.

joncorrin avatar joncorrin commented on July 20, 2024

Just added some more logs and getting same error. I dont think its the window object as its returning true prior to the gtag error:

Here is what was logged:

GTM-WBJL4SD
main-es2015.932a9cccfc421ae0b4a2.js:1 true "GTM-WBJL4SD"

Here us the code

let gtmId = '';

if (window.location.href.toString().includes('gtagId')) {
  const vars = {};
  const parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
    return vars[key] = value;
  });
  gtmId = vars['gtagId'];
  console.log(gtmId);
}

console.log(window.location.href.toString().includes('gtagId'), gtmId);

from angular-google-tag-manager.

joncorrin avatar joncorrin commented on July 20, 2024

https://xilo-staging-forms.herokuapp.com/client-app/form/page/start?companyId=955987&[email protected]&formId=95&clientId=20505&gtagId=GTM-WBJL4SD

from angular-google-tag-manager.

joncorrin avatar joncorrin commented on July 20, 2024

Do you have any other suggestions on how to make the gtagId dyanmic?

from angular-google-tag-manager.

mzuccaroli avatar mzuccaroli commented on July 20, 2024

if you build your production code and run it from your dist folder you have the same issue?

also a good (and safer) idea to make your gtmId dynamic is retrieve it from a dedicated API or a AWS Systems Manager Parameter Store, than leaving it on on query string

from angular-google-tag-manager.

joncorrin avatar joncorrin commented on July 20, 2024

Same error when building in production ng serve --prod. When I run ng serve without the --prod flag it works fine. I cant nail down why this is happening.

from angular-google-tag-manager.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.