Sanjoy
Sanjoy

Reputation: 336

Is there a way that javascript reads its own variables and generates datalayer variables via GTM scripts only?

<script>
    dataLayer = [{
      'pageCategory': 'signup',
      'visitorType': 'high-value'
    }];
</script>

Upvotes: 0

Views: 73

Answers (2)

Here is a simple example of how to do it. Create a "Custom HTML tag" and insert in it:

<script>
    dataLayer.push("documentTitle", document.title);
    dataLayer.push("event", "documentTitleReady");
</script>

Now you can use this event to fire another tag that reads this dataLayer variable.

Upvotes: 0

MarkeD
MarkeD

Reputation: 2631

You can use a GTM variable that reads a global JavaScript variable on your page, yes. Then you can use that GTM variable in a custom HTML tag to push to the dataLayer. Make sure it also fires a "im-ready" event too though, so any dependent tags can wait for it to fire.

Upvotes: 1

Related Questions