partytown icon indicating copy to clipboard operation
partytown copied to clipboard

Support for Google Tag Manager Community Templates (Sandboxed JS)

Open klunejko opened this issue 3 years ago • 0 comments

I tried converting our existing GTM setup to run through partytown, however I ran into issues with the majority of our tags. Official templates worked as expected (Google Adwords, Analytics etc.), as do Custom HTML/Image tags. The problem is with Community Templates which are already sandboxed by GTM. Functionality isn't affected (these tags can still inject scripts), however window variables aren't being forwarded from GTM to the sandboxed tag:

partytown = {
  forward: [ 
    'dataLayer.push', 'fbq' 
  ] 
};

This works fine and fbq can be accessed if the fb pixel is included directly on the page or via GTM using the Custom HTML tag.

Same goes for dataLayer variables that are set when configuring the template.

Code within GTM Template

// data.value = dataLayer.ecommerce.value (forwarded from partytown)
const value = data.value; // works

// sandbox within a sandbox
let fbq = copyFromWindow('fbq'); // This is where it breaks down

I couldn't find details regarding the sandbox implementation that GTM uses, so I'm not sure if this is something that can be solved or not. Alternatively we could always rewrite the templates as custom html tags, though ideally I would like to avoid having duplicate tags instead of a single template.

Info regarding the sandbox can be found here: https://developers.google.com/tag-platform/tag-manager/templates/sandboxed-javascript

Thanks in advance.

klunejko avatar Feb 16 '22 18:02 klunejko