epool
epool

Reputation: 304

Google Tag Manager Template injectScript with Data Attribute

How can I create a GTM template that loads a pixel code with a data attribute? This particular pixel needs a data attribute to include the API Key in order for the pixel to run on the site.

I've added the code from the pixel to the template in this example, however this does not appear to be how Sandbox Javascript is supposed to be used to inject the JS.

// Access Tag Variables
var apikey = data.apikey;

// Access Document Object
var document = require('document');

// Create New Script
var Script = document.createElement('script');

// Update New Script Attributes
Script.type = 'text/javascript';
Script.src = 'https://cdn.pixel.com/js/pixel.min.js';
Script.setAttribute('token', apikey);
document.getElementsByTagName('head')[0].appendChild(Script);

// Call data.gtmOnSuccess when the tag is finished.
data.gtmOnSuccess();

Alternatively it looks like I should be using injectScript like below, however the documentation does not explain how to include a data attribute.

injectScript('https://cdn.pixel.com/js/pixel.min.js', data.gtmOnSuccess, data.gtmOnFailure);

Upvotes: 3

Views: 880

Answers (1)

ptts
ptts

Reputation: 2086

Unfortunately, it is currently not possible to add custom attributes to scripts added with the sandboxed injectScript method.

Upvotes: 2

Related Questions