1

I have a very odd issue happening that I have been struggling with and can't find answer to. We have a 3rd party module to add GTM to the site and after doing a deploy the GTM is fine. After a period of time sometimes a few minutes and sometimes an hour or more the url for the JavaScript gets stripped out of the html but the GTM snippet is still there but just no url for the GTM JavaScript file. This is what I mean, this is what it starts out as.

<script>
    requirejs([], function() {
    (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','xxx-xxxxxxx');});
</script>

This is what is in the html after the url gets stripped out.

<script>
    requirejs([], function() {
    (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=
    '';f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','xxx-xxxxxxx');});
</script>

I am running Magento Community 2.3.6. I have tried disabling the Magento_Csp module. I have added the googletagmanager.com domain to the allowed list. I have the Mage Plaza Security Pro module and have tried disabling it. Nothing seems to make a difference. I have tried disabling the GTM module and putting the GTM snippet in the html head and the same thing happens.

I have been working with the hosting provider support and they have not been able to come up with anything either.

Anyone have any ideas at all?

7
  • It's impossible to tell from this information alone. You should debug the "3rd party module" that adds GTM code to your site.
    – Willem
    Commented Jun 25, 2021 at 8:48
  • But even when I add the GTM snippet to the html head in the admin the url is still being stripped out.
    – Trevor Orr
    Commented Jun 25, 2021 at 12:29
  • First determine if your server strips the url when rendering. If not, use the devtools JavaScript debugger to determine where the url is being removed.
    – Willem
    Commented Jun 25, 2021 at 19:32
  • Hosting provider says they have nothing in place that would do something like that. When I use 'View Page Source" a the browser that is going to show what the server returns right ? It shows the html before any JavaScript has modified it right? When I do "View Page Source" the url is stripped out.
    – Trevor Orr
    Commented Jun 25, 2021 at 23:18
  • You still need to deduce where the transformation happens. Browser extension, proxy/cdn/waf, server? What your provider says is secondary info, you should verify that yourself.
    – Willem
    Commented Jun 26, 2021 at 7:00

1 Answer 1

0

In our case, this unexpected stripping was caused by the Amasty GDPR module, that (roughly put) decides during the execution of vendor/amasty/gdpr-cookie/Plugin/ProcessPageResult.php that if under some conditions (related to the _ga cookie not being in an "accepted" cookie group) it must strip away the google tag manager url and replace it with an empty string.

This is the regex expression that uses:

public const GOOGLE_TAG_MANAGER_REG = '/\'https:\/\/www\.googletagmanager\.com\/gtm\.js\?id=.*?;/is';

Here is where the replacement takes place (line 96):

if (preg_match(self::GOOGLE_TAG_MANAGER_REG, $output, $match)) {
    $output = preg_replace(self::GOOGLE_TAG_MANAGER_REG, "'';", $output);
    $response->setBody($output);
}

A simple workaround (if you want to load tag manager for any user in all cases, which might not be GDPR compliant...) is to modify the tag manager snippet to make the regex fail to match, for example by adding an additional/trivial concatenation to the url:

j.src='h'+'ttps://www.googletagmanager.com/gtm.js?id='+i+dl;

Although, you might want figure out under which exact conditions Amasty is running the replacement and try playing with the module cookies config instead (in a GDPR compliant way, if needed).

AFAIK the reason why a user with (for example) all cookie groups accepted might have tag manager disabled in some pages (!!!) is due to the fact that Amasty is modifying the html under certain conditions (but not touching the X-Magento-Vary cookie accordingly), which causes the full page cache system (e.g. Varnish) to serve cached versions of pages generated with some cookie preferences to users with other cookie preferences. A quick workaround for this is to add the relevant Amasty cookie value amcookie_allowed to the Varnish vcl hash subroutine (fortunately that cookie has a limited number of possible values, proportionally to how many cookie groups you have configured).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.