我希望我的javascriptcode能够捕获文本" Personenverzorging"和" Gespecialiseerde voetverzorging"在我的Google Analytics自定义变量中使用。
此文本在每个页面上都会更改,因此javascript无法搜索确切的术语,但它应该知道HTML标记的位置。
<div class="views-field views-field-name">
<span class="field-content"><a href="/opleidingen?f%5B0%5D=field_taxonomy_sector%3A23">Personenverzorging</a></span>
</div>
<div class="views-field views-field-name-2">
<span class="field-content"><a href="/opleidingen?f%5B0%5D=field_taxonomy_sector%3A84">Gespecialiseerde voetverzorging</a></span>
</div>
这是我将使用Google Tagmanager在我的网站上实施的Google Analytics代码。
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #2. Required parameter.
'Sub-Section', // The 2nd-level name for your online content categories. Required parameter.
'Fashion', // Sets the value of "Sub-section" to "Fashion" for this particular article. Required parameter.
3 // Sets the scope to page-level. Optional parameter.
]);
子节应替换为&#34; Personenverzorging&#34;和#34; gespecialiseerde voetverzorging&#34;。
这是我的代码。但它不起作用。有人能引导我走向正确的方向吗?
我可以使用通配符,因为页面中唯一的唯一类是views-field-name?
var elements = document.getElementsByClassName("views-field-name-*");
var string;
string = "'_setCustomVar',1"
for (var i = 0; i < elements.length; i++) {
string = string + ","+ elements[1].innerText;
string = elements[1].innerText;
document.write(elements[1].innerText);
}
非常感谢!感谢您的帮助。
答案 0 :(得分:1)
如果您使用的是Google跟踪代码管理器,我会使用dataLayer使用dataLayer变量传递页面元素。从页面到页面,它将更加一致。这就是Google跟踪代码管理器的构建方式。
例如,我将使用&#39;子部分&#39;和&#39;时尚&#39;因为我不了解荷兰语或您网站的层次结构,但这应该足以让您入门。
1)创建两个新的dataLayer变量marcros:一个用于子节,一个用于子节。
将它们称为subSection和subSubSection,并确保它们是dataLayer的第1版。
2)将dataLayer对象添加到您的网站。看起来应该是这样的:
<head>
dataLayer = [{
'subSection': 'Personenverzorging',
'subSubSection': 'gespecialiseerde voetverzorging'
}];
</head>
<body>
GTM CONTAINER CODE
</body>
3)现在,为两个&#39; subSection&#39;传播你想要的值。和&#39; subSubSection&#39;使用CMS的全局变量。这应该发生在服务器端。因此,当页面加载时,值将在那里。
4)一旦你看到值正在通过(你可以查看页面的源代码或在开发控制台中输入dataLayer来检查对象),你就可以准备好设置自定义变量。 / p>
听起来,您希望在每个页面上触发CV。请返回GTM,转到Google Analytics综合浏览量跟踪类型,然后转到更多设置&gt;自定义变量&gt;新的自定义变量。
创建新的自定义变量,应该有两个:一个用于subSection,另一个用于subSubSection。选择您的插槽(1-5),添加您的名称,如Sub Section,然后为该值,单击building-block并选择{{subSection}}
宏,然后设置范围。发布容器,并在GA中查看数据。