如何访问Google标签管理器中WordPress中创建的自定义分类学?

发布于 2025-02-11 05:24:03 字数 715 浏览 2 评论 0原文

我在WordPress中为文章长度创建了自定义分类学。它称为“长度”,这些是其中的项目: image

我还创建了一个自定义维度在Google Analytics(索引5)中,并在Google标签管理器中为其创建了一个自定义数据层变量。但是我认为我正在错误地引用数据层变量名称,当我在GTM中预览时,它是“未定义”的。

当我检查页面时,我可以在类末尾看到分类的“长度”,并且是分类的“标准”,但是我无法弄清楚数据层变量名称是什么(我知道我需要使用点表示法) 。

这是我检查时显示的代码。

<article id="post-2784" class="post post-2784 type-post status-publish format-standard has-post-thumbnail hentry category-deforestacion category-gran-chaco length-standard">
...
</article>

如何正确访问GTM的分类法?我敢肯定,仅仅是不正确的,并且花了几个小时进行调试和研究,但无法弄清楚。

I created a custom taxonomy in WordPress for article lengths. It's called 'Length' and these are the items within it: image

I also created a custom dimension in Google Analytics (it's index 5), and created a custom Data Layer variable in Google Tag Manager for it. But I think I am referencing the Data Layer Variable Name incorrectly and it is 'undefined' when I preview it in gtm.

I can see the taxonomy 'length' and it's classification 'standard' at the end of the class when I inspect the page, but I can't figure out what the Data Layer Variable Name is (I know I need to use dot notation).

Here's the code that shows when I inspect.

<article id="post-2784" class="post post-2784 type-post status-publish format-standard has-post-thumbnail hentry category-deforestacion category-gran-chaco length-standard">
...
</article>

How do I access the taxonomy correctly for gtm? I'm pretty sure it's just this that is incorrect and spent hours debugging and researching but can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2025-02-18 05:24:03

Datalayer是您页面上的全局变量。尝试在文章页面上打开Dev Console,并在其中输入Datalayer。我非常怀疑它的居民,除非您的分类扩展超过90%的扩展名。

您不需要DL。如果要在页面视图上填充自定义维度,只需拥有一个自定义JS代码变量,该变量看起来像这样:

function(){
   return document.querySelector(".status-publish.post").className.split(' ').find(function(x) {x.startsWith('length-')});
}

然后只使用它而不是DL变量。它首先使用.status-publish.post在页面上的任何内容,并返回具有长度 - 其中的第一类。逻辑假设某些事情。您应该尝试将其调整为页面设计,尤其是在页面上有多个.status-publish.post.post项目时。

dataLayer is a global variable on your pages. Try opening dev console on the article page and type dataLayer in it. I heavily doubt it's populated though unless your the taxonomy extension is better than 90% of extensions out there.

You don't need a DL though. If you want to populate your custom dimension on the page view, just have a custom JS code variable that would look something like this:

function(){
   return document.querySelector(".status-publish.post").className.split(' ').find(function(x) {x.startsWith('length-')});
}

And then just use it instead of the DL variable. It takes whatever first has .status-publish.post on the page and returns the first class that has length- in it. The logic assumes certain things. You should try and adjust it to your page design, especially if there are multiple .status-publish.post items on the pages.

甜心 2025-02-18 05:24:03

我没有完全尝试建议的内容,但是对于那些想知道这对我有用的人:

function(){
  
  var length = document.querySelector(".status-publish.post").className.split('length-')[1].split(' ')[0];
 return length; 

}

我遇到了一个问题,它也会跟踪主页上的尺寸(由于WordPress主题),所以我将其包裹在一个IF中:

    function(){
  var pagePath1 = document.location.pathname.split('?')[0];
  
  if (pagePath1 == '/' ){
} else {
  
  var length = document.querySelector(".status-publish.post").className.split('length-')[1].split(' ')[0];
 return length; 
}
}

它与我在Google Analytics(分析)和WordPress中的自定义分类法中的自定义维度完美搭配!

I didn't try exactly what was suggested but for those wondering this is what worked for me:

function(){
  
  var length = document.querySelector(".status-publish.post").className.split('length-')[1].split(' ')[0];
 return length; 

}

I had an issue where it would track the dimensions on the homepage too (due to the wordpress theme), so I wrapped it in an if:

    function(){
  var pagePath1 = document.location.pathname.split('?')[0];
  
  if (pagePath1 == '/' ){
} else {
  
  var length = document.querySelector(".status-publish.post").className.split('length-')[1].split(' ')[0];
 return length; 
}
}

It works perfectly with my custom dimensions in Google Analytics and my custom taxonomies in Wordpress!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文