You are here: Home | Wordpress | Track WordPress Post topics in Google Analytics using TrackEvent

Track WordPress Post topics in Google Analytics using TrackEvent

by Jared Huber on March 15, 2010

Just a quick post describing how I’m using Google Analytics’ _trackEvent function within WordPress. At a high-level, I’m tagging posts using WordPress’ built-in capabilities, then capturing those using server-side code and storing them in Google Analytics Event tracking.

Google’s documentation decribes how to track a PHP site using Google Analytics.

Create the external PHP file containing the Analytics tag.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
</script> <?php
// End Analytics tracking code
?>

Call the external file using an include.

<?php include ("analyticsTracking.php"); ?>

WordPress has some great built-in functions that will tell you information about a given post. For example, the wp_list_categories function will return the category to which a given post belongs. Similarly, wp_get_the_tags will return every tag associated with a post, delimited by your choice of delimiter. This is pretty slick; as the author of a post, you have the ability to define which tags you’d like to associate with the post. These could be topics, product categories, months, etc. Get creative! We’re going to add a call to this function within the external php file, and track the post tags using Google Analytics trackevent function.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
</pre><pre>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxxxx-x");
pageTracker._trackPageview();
<?php $posttags = get_the_tags();
     if ($posttags)
     {
        foreach($posttags as $tag)
        {
        echo "pageTracker._trackEvent('Post Tag','View','$tag->name');";
        }
    } ?>
</script> <?php
// End Analytics tracking code
?>
</div>

This code iterates through all of the post’s tags and calls a new trackevent for each. Here is what the output looks like in Google Analytics reporting.

Wordpress / Google Analytics Event Tracking

Wordpress / Google Analytics Event Tracking

How about using the get_the_author function to populate the event with author name, or the get_lastpostdate to determine when the post was published?

Related posts:

  1. Must-have SEO WordPress plugins
  2. How not to move your WordPress blog from one host to another
  3. Matched Search Query in Google Analytics
  4. Tracking Full Referral URLs in Google Analytics
  5. Visitor Segmentation based on Stage of the Marketing Funnel

{ 1 comment… read it below or add one }

Marta Biets December 4, 2010 at 2:02 pm

You can also use a smart function into the functions.php of your theme. just insert a small snippet and go.

here is a example http://wordpressfunctions.com/performance-and-tracking/statistics/google-analytics-integration/

Leave a Comment

{ 1 trackback }

Previous post:

Next post: