Zend certified PHP/Magento developer

Magento 2.4.4p4 EE – GTM Session Regeneration on Every Page Load

I’m encountering a discrepancy between user/session activity and transaction count in Google Analytics (GA) for a Magento 2.4.4p4 Enterprise Edition project. The culprit seems to be frequent GTM session regeneration triggered by the GTM tags script for the datalayer.

Here’s a breakdown of the situation:

  1. GTM Tag Implementation:
  • Installed GTM tags with the script placed in the source code (app/design).
  • Observed a significant discrepancy between user/session and transaction data in GA.
  1. Troubleshooting Steps:
  • Attempted to modify the script initialization location from app/design to app/code through default.xml.
  • Issue persisted.
  1. Code Snippet:
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
  1. Requirement and Observation:
  • GTM requires the script at the head of every page for proper loading.
  • Script presence leads to session regeneration on every page change.
  • Script absence prevents datalayer push to GTM.

Question:

I don’t fully grasp how this script interacts with Magento’s session regeneration mechanism. However, it appears to be the only factor I can manipulate.

To further investigate:

  • I replicated the issue on a fresh Magento 2.4.4p4 EE installation.
  • This suggests a potential Magento or GTM script-related issue, but I’m unsure of the root cause.

Request:

I’d greatly appreciate any insights or solutions to resolve this session regeneration problem and ensure accurate data collection in GA.