Global site tag (gtag.js) - Google Analytics and aurelia

Does anyone have experience with Global site tag (gtag.js) - Google Analytics under Aurelia.

I created a function to push data to the dataLayer object like below

public send_screen_view_gtag(name: string): void {

    if (!(<any>window).dataLayer) {
      (<any>window).dataLayer = [];
    }
    console.log((<any>window).dataLayer);
    
    (<any>window).dataLayer.push('js', new Date());
    (<any>window).dataLayer.push('config', 'G-3NH3SR7B1N');


    (<any>window).dataLayer.push('config', 'G-3NH3SR7B1N', {
      'page_title' : name,
      'page_path': '/' + name
    });

    console.log((<any>window).dataLayer);
}

The idea was to call this function from attached on various pages.

I also placed <script async src="https://www.googletagmanager.com/gtag/js?id=G-3NH3SR7B1N"></script> in my index.ejs file

The trouble is the content of dataLayer never seems to be read or sent to google.

Does anyone have any idea what todo with this ?

I assumed adding <script async src="https://www.googletagmanager.com/gtag/js?id=G-3NH3SR7B1N"></script> would setup whatever google does to read dataLayer.

Thanks

1 Like

I guess what you’re running into is that the body gets replaced by Aurelia as described here https://stackoverflow.com/questions/46224130/google-tag-manager-preview-mode-does-not-work-in-aurelia

Also try to google for Aurelia and tagmanager and you should find a few plugins which seem to do all that’s needed

1 Like

I did seem to get it going by setting pushState to true. Not sure why that helps though.

1 Like