wordpressgoogle-analyticsgoogle-tag-managerdata-layer

Tracking WP Post Tags using Google Analytics and dataLayer (GTM)


I set the dataLayer variable with the post author, tags and categories data. All these were already created as custom dimensions on Analytics.

The problem is that Post Tags and Categories are arrays, Do I have to split the arrays on Google Tag Manager? (Or something like that, IDK). I did a test with Post Categories setting up pageCategory[0] as Data Layer Variable Name (Because I need only the first category) but It's not working good.

The dataLayer looks like this:

{
"pagePostType":"post",
"pagePostType2":"single-post",
"pageCategory":["ha-kidd-knitting-supplies"],
"pageAttributes":["big-time-yarn","charles-voth","classic-shades","easy-knits","km066","knitted-cowl","textured-cowl","universal-yarn"],
"pagePostAuthor":"Charles Voth"
}

What is the best way to collect all the wordpress post tags and categories as custom dimensions on Analytics?


Solution

  • You need to use dot notation to access the array, so your page cateogry dataLayer variable could have a value like:

    pageCategory.0
    

    For your page attributes, they could be accessed individually:

    pageAttributes.1
    pageAttributes.2
    

    etc.

    Of course with multiple page attributes, this wouldn't be the best way. You should instead define your dataLayer variable to have a value of just pageAttributes, and then you can loop through this variable to populate your Custom Dimensions, and then concatenate the strings.