0

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.

Ld91049
  • 1
  • 3

2 Answers2

0

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.

BNazaruk
  • 6,300
  • 3
  • 19
  • 33
  • Thanks! I will try this and see if it works. Will follow up either way. – Ld91049 Jul 01 '22 at 14:22
  • I attempted this but get the following error: "This language feature is only supported for ECMASCRIPT_2015 mode or better: arrow function". Assumingly because of the '=>' on Line 2. Tried a few workarounds but nothing works so far. Any idea? – Ld91049 Jul 06 '22 at 12:29
  • Yes, sorry, forgot about Google being unable to move past 2015. Updated the code. Should work now. – BNazaruk Jul 06 '22 at 14:03
  • I found a solution: function(){ var classarray = document.querySelector(".status-publish.post").className.split(' '); var splitword = classarray[9]; var length = splitword.split('-')[1]; return length; } Need to figure out how to exclude certain pages (or filter the data after) but it works! – Ld91049 Jul 06 '22 at 17:51
0

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!

Ld91049
  • 1
  • 3