In HTML Datalayer output snippet is at wrong location before body closing

Created on 25 October 2023, 8 months ago
Updated 27 November 2023, 7 months ago

Problem/Motivation

The datalayer out is before closing</body>at wrong location. It should be just after <head/> in html.

  <script>window.dataLayer = window.dataLayer || []; window.dataLayer.push({"drupalLanguage":"en","drupalCountry":"US","siteName":"Umami Food Magazine","entityCreated":"1698170269","entityLangcode":"en","entityStatus":"1","entityUid":"4","entityUuid":"5fef7eca-f9c2-4214-9b98-7e060202b9a8","entityVid":"12","entityName":"Umami","entityType":"node","entityBundle":"recipe","entityId":"6","entityTitle":"Gluten free pizza","field_number_of_servings":{"value":"4","_attributes":[]},"field_preparation_time":{"value":"15","_attributes":{"content":"15"}},"field_tags":[],"entityTaxonomy":{"recipe_category":{"31":"Main courses"}},"userUid":0});</script>

  </body>

Steps to reproduce

tested on Drupal 10.1.5 and Datalayer 2.0.1

Proposed resolution

In html put the Datalayer script just after head

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States adanbouzoua

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @adanbouzoua
  • Assigned to shiv_yadav
  • Issue was unassigned.
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States adanbouzoua

    The version 2.0.0 has not bug related to snippet placement.
    The bug was introduced with the version 2.0.1

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States eojthebrave Minneapolis, MN

    I bet this is what changed ... moving to using hook_page_bottom to attach the code. https://git.drupalcode.org/project/datalayer/-/commit/80a6a9e35968ca9760...

    If you change that to use hook_page_top instead of hook_page_bottom does it fix the problem?

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States adanbouzoua

    Update
    The previous test was done with Google Tag Version: 8.x-1.6 and Datalayer 2.0.1
    Changing Google Tag Version: 8.x-1.6. >> Version: 2.0.2 fix the issue

    Conclusion: Datalayer 2.0.1 + Google Tag 2.0.2 has no issue. The module works with Google Tag version 2 but not with Google Tag version 8
    ---
    dataLayer.push allows the snippet elsewhere than inside .
    It seems that it has been implemented on Google Tag 2.0.2 but not 8.x-1.6

    Data Layer declaration vs dataLayer.push
    By adding a dataLayer snippet above the GTM container snippet. This step is called โ€œData Layer declarationโ€.
    Or by pushing data with dataLayer.push method.
    https://www.analyticsmania.com/post/what-is-data-layer-in-google-tag-man...

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mihaic

    I propose to update the module page documentation and also the project readme file to reflect what #6 is saying, here a patch, thanks

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States eojthebrave Minneapolis, MN

    Good discovery. Thanks for digging into this more. I added a note to the project page about compatibility with Google Tag module versions.

    I wonder if there's a way to add something to the composer.json file, or the .info.yml file for the module that would also declare this requirement. So that for example Composer would let you know that you have incompatible modules? I think something like that, plus a note in the README, would make the most sense.

Production build 0.69.0 2024