[英]Google analytics custom variables are not working when placed page view tracking in html file and variable tracking in javascript file
[英]Get page text using javascript for Google Analytics custom variable
我希望我的javascript代碼捕獲文本“ 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.
]);
小節應替換為“ Personenverzorging”,而“時尚”替換為“ gespecialiseerde voetverzorging”。
這是我的代碼。 但這行不通。 有人可以引導我朝正確的方向前進嗎?
由於頁面內唯一的唯一類是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);
}
非常感謝! 我感謝您的幫助。
如果您使用的是Google跟蹤代碼管理器,則可以使用dataLayer通過dataLayer變量傳遞頁面元素。 從頁面到頁面,這將更加一致。 排序是Google跟蹤代碼管理器的功能。
例如,由於我不了解荷蘭語或網站的層次結構,因此我將使用“子部分”和“時尚”,但這足以使您入門。
1)創建兩個新的dataLayer變量marcros:一個用於Sub-section,一個用於sub-sub-section。
將它們稱為subSection和subSubSection之類的名稱,並確保它們是dataLayer的版本1。
2)將dataLayer對象添加到您的網站。 它看起來應該像這樣:
<head>
dataLayer = [{
'subSection': 'Personenverzorging',
'subSubSection': 'gespecialiseerde voetverzorging'
}];
</head>
<body>
GTM CONTAINER CODE
</body>
3)現在,使用CMS的全局變量傳播“ subSection”和“ subSubSection”所需的值。 這應該在服務器端發生。 因此,當頁面加載時,值將在其中。
4)一旦看到值通過(您可以查看頁面的源代碼或在開發控制台中鍵入dataLayer來檢查對象),就可以設置自定義變量了。
從表面上看,您會希望CV在每個頁面上觸發。 因此,請返回GTM,然后轉到您的Google Analytics(分析)綜合瀏覽量跟蹤類型,然后轉到“更多設置”>“自定義變量”>“新自定義變量”。
創建新的自定義變量,應該有兩個:一個用於subSection,一個用於subSubSection。 選擇您的廣告位(1-5),添加您的名稱(如“子節”),然后為該值添加值,單擊構件,然后選擇{{subSection}}
宏,然后設置范圍。 發布容器,並在GA中查看數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.