Search code examples
universal-analytics

How to send category name from Wordpress to Google Analytics Content Groups (universal tracking code)


I want to enable content grouping in Google Analytics by submitting the category of my posts from within the tracking code.

A solution for GA's async version of the tracking code would look like this:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
<?php 
if (is_single()){
    echo “_gaq.push(['_setPageGroup', 1, '".get_the_author()."']);\n”;
    $category = get_the_category();
    if ($category && !empty($category[0]->cat_name)){
        echo “_gaq.push([‘_setPageGroup’, 2, ‘”.$category[0]->cat_name.”‘]);\n”;
    }
}
?>
_gaq.push(['_trackPageview']);

How can I set up the same functionality using GA's universal tracking code?

Thanks!

Best regards, Alex


Solution

  • Classic GA syntax:

    _gaq.push(['_setPageGroup', '<Index Number>', '<Group Name>']);
    

    UA syntax:

    ga('set', 'contentGroup<Index Number>', '<Group Name>');
    

    So in your case it should/would/could be this:

    <?php 
       if (is_single()){
          echo "ga('set', contentGroup1, '".get_the_author()."');\n";
          $category = get_the_category();
          if ($category && !empty($category[0]->cat_name)){
             echo "ga('set', contentGroup2, '".$category[0]->cat_name."');\n”;
          }
       }
    ?>
    

    More information can be found here: https://support.google.com/analytics/answer/2853546?hl=en