我有一个单页应用程序,我需要在Google标签管理器中设置一个变量。我正在对页面视图触发器使用历史记录更改。
因此,数据层需要在加载和每个历史更改事件时被推送。该变量应列出用逗号分隔的所有innerText类x。
这个是可能的吗?下面只是在窗口加载时获取第一个div类x innertext。
Html
<div class="x"> tag 1</div>
<div class="x"> tag 2</div>
<div class="x"> tag 3</div>
<div class="x"> tag 4</div>Google标记管理器变量
function() {
return window.document.getElementsByClassName('x')[0].innerText;
}变量输出:标签1,标签2,标签3,标签4
我想将该变量添加为页面视图的自定义维度。
发布于 2018-08-02 18:16:10
您可以使用自定义JavaScript变量来执行此操作。下面这样的代码应该能起到作用:
function(){
// Empty array that will store your comma separated list
var myVariable = [];
// Create array of all elements with class "x"
var myArray = Array.from(document.querySelectorAll('.x'));
// Loop through myArray to get text and push to comma separated list
myArray.forEach(function(element){
myVariable.push(element.innerHTML);
})
// Return comma separated list
return myVariable;
}然后,您可以使用此变量作为自定义尺寸的值。
https://stackoverflow.com/questions/51630930
复制相似问题