anasey
anasey

Reputation: 196

Is there a way to include data-* as a script attribute in custom Google Tag Manager

I'm including a Custom script in GTM that makes a call to an external resource. The script looks like this

<script type="text/javascript" id="some-key" data-key="xxxxxxxxxxxxxxxxxx" src="link-to-external-resource"></script>

I've set its triggering to All pages which currently triggers fine But the problem is, the script renders without the data-key attribute in the DOM. It's rendering in the DOM looks like this

<script type="text/javascript" id="some-key" src="link-to-external-resource"></script>

Am I missing something? Is there a way to include data-* attributes in Google Tag Manager?

Upvotes: 6

Views: 2047

Answers (1)

Дмитро Булах
Дмитро Булах

Reputation: 3847

I can confirm that for unknown reasons GTM strips the data attributes from appended script elements. Other elements like img seems to keep data attributes. I've tested some workaround that preserves the data attributes but it's necessary to test if the appended script behaves properly when added in this way:

<script>
  var script = document.createElement('script');
  script.id = 'Gdf234ds'
  script.dataset.key = 'xxxxxxxxxxxxxxxxxx'
  script.src = "https://link-to-external-resource.js"

  // use another target than head if necessary
  document.getElementsByTagName('head')[0].appendChild(script) 
</script>

Upvotes: 7

Related Questions