mzuccaroli / angular-google-tag-manager

A service library for integrate google tag manager in your angular project
https://itnext.io/how-to-add-google-tag-manager-to-an-angular-application-fc68624386e2
MIT License
136 stars 78 forks source link

Multiple gtm.js loaded #107

Closed chuhancheng closed 10 months ago

chuhancheng commented 2 years ago

If we continuously call pushTag many time before js loaded. It would cause multiple js loading and these async js loading would cause race condition let our event order incorrect.

mzuccaroli commented 2 years ago

i think you have some issues in the library initialization did you followed this tutorial? https://itnext.io/how-to-add-google-tag-manager-to-an-angular-application-fc68624386e2 can you provide some code snippets of your configuration?

chuhancheng commented 2 years ago

Sorry for my late reply:( If we call this.gtmService.pushTag two times after app initialized.

Screen Shot 2021-12-20 at 2 47 41 PM

This code would lead addGtmToDom be called two times and then doc.createElement would possibly be trigger two times. so that there would have two js be loaded.

mzuccaroli commented 2 years ago

the problem is in the push tag function , you don't need to use the add gtm to dom it like that, take a look to this tutorial or to the demo application included in the repositopry: https://github.com/mzuccaroli/angular-google-tag-manager/blob/master/demo-application/src/app/app.component.ts

chuhancheng commented 2 years ago

I had provide a sample PR. Currently we have to check whether GTMscript has loaded before we send another event.

phatyh commented 2 years ago

hi @chuhancheng its my solution;

my gtm service code;

customEvent(data: IData): void {
  this.send('customEvent', data);
}

pageView(data: IData): void {
  this.send('pageView', data);
}

async send(event: string = 'pageView', data: IData): Promise<void> {
  const gtmTag = {
    event,
    ...data
  };

  await this.gtmService.pushTag(gtmTag);
}

any trigger

this.gtmService.pageView({pageName: event.url});

other any trigger

this.gtmService.customEvent({
  data,
});

hi @mzuccaroli , i think update doc. btw thx to package.

thx.

chuhancheng commented 2 years ago

hi @chuhancheng its my solution;

my gtm service code;

customEvent(data: IData): void {
  this.send('customEvent', data);
}

pageView(data: IData): void {
  this.send('pageView', data);
}

async send(event: string = 'pageView', data: IData): Promise<void> {
  const gtmTag = {
    event,
    ...data
  };

  await this.gtmService.pushTag(gtmTag);
}

any trigger

this.gtmService.pageView({pageName: event.url});

other any trigger

this.gtmService.customEvent({
  data,
});

hi @mzuccaroli , i think update doc. btw thx to package.

thx.

Thanks.

candidJ commented 2 years ago

I ran into same issue as well. Did the solution work for you @chuhancheng ?

candidJ commented 2 years ago

hey @mzuccaroli , any solution or workaround for the problem. I went through the tutorial only to find out multiple tags loaded in demo image. Kindly refer attached.

Screenshot 2022-03-14 at 11 35 00 PM
korneliusz-w commented 2 years ago

Just do in constructor on onInit hook more addTag.push and GTM.js will load many times.

` ngOnInit() { const gtmTag = { event: 'button-click', data: 'my-custom-event', }; this.gtmService.pushTag(gtmTag);

const gtmTag2 = {
  event: 'button-click2',
  data: 'my-custom-event',
};
this.gtmService.pushTag(gtmTag2);`

image

But maybe this is not a bug https://www.analyticsmania.com/post/multiple-installations-of-google-tag-manager-detected/

paolostefan commented 10 months ago

@mzuccaroli if this isn't a real issue, shouldn't it be closed?