Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I was needed to set different dataLayer variable for different pages before Google Tag init. By default dataLayer is cached and the same on all pages. I couldn't find any hook or methods to alter dataLayer. So I added hook to alter dataLayer before attaching this variable on the page. May be it's not the best place to alter dataLayer, because you need to create text for javascript variable. It will be better to alter array for dataLayer.
Comment | File | Size | Author |
---|---|---|---|
#2 | google_tag-add-hook-to-alter-data-layer-2851359.patch | 2.19 KB | levmyshkin |
Comments
Comment #2
levmyshkinI used this patch:
Comment #3
solotandem CreditAttribution: solotandem commentedI agree with you it would be preferable to edit the array not the string.
The patch does not provide anything more than can already be done with hook_page_attachments_alter().
Both hooks would involve editing the JS string.
As on-the-fly changes to datalayer seem to be the purview of the datalayer project, perhaps you could use it for this purpose.