We are setting up some server-side Google Analytics events and we recently transitioned our GA implementation to use Google Tag Manager. I would like to maintain some of the constants we use in GTM so that they can be controlled from there. A good example is which GA Tracking ID to use. We currently store this in a lookup field called "GA - Tracking ID Lookup".
We would like to find a way to retrieve the value of this GTM variable in javascript code on our site. Is this possible?
答案 0 :(得分:1)
我认为,管理此问题的最佳方法是使用Data Layer。您的JS将能够读取数据层中的值,GTM也将能够读取。
如果要走这条路,可以将查询变量更改为GTM的数据层变量。
示例:
让您的服务器写出DOM元素以包含类似以下内容的分析跟踪ID
<meta name="ga-tracking-id" content="UA-1234567890-1" />
您还可以将相同的信息存储到dataLayer中:
<script>
dataLayer = [{
'ga-tracking-id': 'UA-1234567890-1',
}];
</script>
然后可以使用GTM中的DataLayer变量类型来检索此数据。
通过一个GTM容器进行管理的另一种方法是在GTM中创建一个lookup table,然后根据哪个主机名使用不同的跟踪ID。
尽管我不确定您如何管理此问题,但将所有容器一起使用而不是在一个容器中更改跟踪ID也可能会更容易。