Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid TagManager.initialize() running many times #112

Open
vanminhquangtri opened this issue Oct 10, 2022 · 8 comments
Open

How to avoid TagManager.initialize() running many times #112

vanminhquangtri opened this issue Oct 10, 2022 · 8 comments

Comments

@vanminhquangtri
Copy link

vanminhquangtri commented Oct 10, 2022

Hello.

In my React component, I have to get gtm-ID from another data which change many time. Each time this data changes, my component render. And each time component renders, function TagManager.initialize runs again, this create duplicate gtm scripts (as attached).

My code:

useEffect(() => {
   if (data.gtm_id){
     TagManager.initialize({
      gtmId: data.gtm_id,
     });
   }
}, [data.gtm_id])

Could you please advise me how to avoid function TagManager.initialize() to run if it already run?

Thank you.

image

@seandelaney
Copy link

+1

I have the same issue

@vanminhquangtri
Copy link
Author

@seandelaney have you found solution?
Currently I have to check if the above script tag already existed, will do nothing.
But I think this solution is not safe because actually I don't know what this library does in the background.

@seandelaney
Copy link

@vanminhquangtri No solution yet

@mouthzipper
Copy link

We have the same issue. Any hacks on this?

@drplauska
Copy link

I'd recommend to just keep track of gtm_id and check if it was previously used before initialising it again

@Sashaejarque
Copy link

There is any solution for this bug?

@gcko
Copy link

gcko commented Aug 10, 2023

@vanminhquangtri Based on what you wrote, the initialize function will be called again if data.gtm_id changes. As written, because you have passed data.gtm_id as a dependency on useEffect, it will be called any time it is modified. A potential solution here would be the following:

useEffect(() => {
   if (data.gtm_id){
     TagManager.initialize({
      gtmId: data.gtm_id,
     });
   }
}, []) // Remove data.gtm_id from the dependencies

@giorgiabosello
Copy link

giorgiabosello commented Jan 15, 2024

+1 on this.

A callback function with onLoad should be helpful. Or the implementation of useScript hook inside the react-gtm module would be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants