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

Set data-layer values before initialization #52

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

mithredate
Copy link
Contributor

@mithredate mithredate commented Feb 26, 2024

It's a common practice to set "Initialization - All Pages" as the trigger for GA4, Meta, and a lot of other tags.
image

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:

  1. Many custom events are used as triggers to push new events to different platforms. Since the main tag is initialized after those pushed events (like add_to_cart, purchase, etc.), properties set on the main google tag are not picked by the event. For instance, setting the "server_container_url" on GA4 google tag will not affect other events being sent to GA4.
  2. The Page View would not we captured if the page fails to fully load.
  3. It's no longer possible to set consent values on the Data Layer before loading tags.

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.
image

Example usage:

GoogleTagManage::set(['email' => '[email protected]']);

This will set the email address before initialization, so it's available in the page view event.

@freekmurze freekmurze merged commit 3581f09 into spatie:main Mar 1, 2024
@freekmurze
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants