Space
Space

Reputation: 2042

Way to set a data attribute or class on tag manager output?

I have Google Analytics loaded through Google Tag Manager.

The output is as follows:

<script type="text/plain" async src="https://www.google-analytics.com/analytics.js"></script>

Is it possible to set either a HTML data attribute or css class on this output somehow in Tag Manager? For example:

<script data-unblock class="unblock" type="text/plain" async src="https://www.google-analytics.com/analytics.js"></script>

The context for this relates to automatic cookie blocking of scripts. My cookie solution allows to unblock scripts based on data attribute or css class.

Upvotes: 0

Views: 85

Answers (1)

BNazaruk
BNazaruk

Reputation: 8111

Sure you can. You have the power to execute custom JS in GTM, hence find any tag and add any data-attribute to it.

You just need to execute a custom HTML tag on pageload, adding an inline script

Here, this code will add your data-unblock attribute:

document.querySelector("script[src^='https://www.google-analytics.com']").setAttribute('data-unblock', "");

And this one will add the class if you like it more:

document.querySelector("script[src^='https://www.google-analytics.com']").setAttribute('class', "unblock")

You can test both right here, on stackoverflow.

Upvotes: 1

Related Questions