Set data-layer values before initialization #52
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's a common practice to set "Initialization - All Pages" as the trigger for GA4, Meta, and a lot of other tags.
Currently, there's no way to push any information to the Data Layer before the initialization event, so we have to use the "Window Loaded" event to trigger those tags, which is too late.
The reasons that it's considered late for the tags to initiate after window load are:
This issue was introduced on 2.6.6...2.6.7. This PR attempts to allow setting values to the data layer by using the set method before initialization by moving parts of the setup before GTM is loaded.
The result is all the values added to the GTM service using the
set
method would be set before everything else in GTM.Example usage:
This will set the email address before initialization, so it's available in the page view event.