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

Question: why are there new script tags created for every event? #3

Closed
thomhos opened this issue Nov 29, 2017 · 6 comments
Closed

Question: why are there new script tags created for every event? #3

thomhos opened this issue Nov 29, 2017 · 6 comments

Comments

@thomhos
Copy link

thomhos commented Nov 29, 2017

It seems that for every event a new script tag is created in the head which reassigns the dataLayer and then pushes the event.

I wonder why this is, is it for SSR compatibility or some other reason?

@alinemorelli
Copy link
Owner

Can you show me how do you implement it?
Do you change dataLayer information for every event or is it reassigning same information?

@thomhos
Copy link
Author

thomhos commented Nov 29, 2017

So basically we do this before bootstrapping the app:

const tagManagerArgs = {
    gtmId: env.GOOGLE_TAG_MANAGER_TOKEN,
    dataLayerName: 'PageDataLayer',
}

TagManager.initialize(tagManagerArgs)

Then later on every route change we do this:

const tagManagerArgs = {
    dataLayer: {
        event: 'VirtualPageview',
            virtualPageURL: window.location.href,
            virtualPageTitle: document.title,
         },
    dataLayerName: 'PageDataLayer',
}
TagManager.dataLayer(tagManagerArgs)

We end up with a new script tag for each time the above is executed. I assume it's by design, but mainly wondering why :)

@Vadorequest
Copy link

@alinemorelli I don't get why his implementation would change this behaviour. Since it's the dataLayer method that creates a new script every time it's called

https://github.com/alinemorelli/react-gtm/blob/master/src/TagManager.js#L46

I don't think that's really a good thing.

@rafaelcavalcante
Copy link

Any update on this subject?

@richardblondet
Copy link

richardblondet commented Jun 4, 2019

https://github.com/alinemorelli/react-gtm/blob/master/src/TagManager.js#L47 the dataLayer appears to be initialized upon gtag first run (docs). It kinda serves as a page load, collects all the parameters and fires.

it is not elegant but does the work

@alinemorelli
Copy link
Owner

Fixed on #22

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

5 participants