Having covered some of the basics of a MixPanel install on a WordPress site, we can now show you some simple ways to improve functionality for multiple pages with minimal coding involved.

MixPanel documentation does a great job of helping you get up and running with MixPanel on nearly any platform. To really take advantage of MixPanel’s features in WordPress, however, requires some basic programming (or copy-pasting, for those less comfortable with PHP or scripts).

The following example from MixPanel’s documentation shows an entry that would forward three data points to MixPanel:

mixpanel.track("Video Play", {"age": 13, "gender": "male"});

This example would tell MixPanel that an event happened, called “Video Play,” and the event was triggered by a 13-year-old male. This is obviously important demographic data, but what is not explained here is how the user’s age and gender was discovered!

There are some basics that can be added to track WordPress-specific variables, depending on which elements you want to track. A common MixPanel script, posted in the footer.php file, might look like this:

<!-- MixPanel Start -->
<script type="text/javascript">
mixpanel.name_tag('<?php global $current_user;
get_currentuserinfo(); echo $current_user->user_login; ?>');
mixpanel.track('Viewed Post', {
    "Title":"<?php single_post_title(); ?>",
    "Author":"<?php the_author(); ?>",
    "Tags":"<?php $posttags = get_the_tags(); $count=0;
   if ($posttags) { foreach($posttags as $tag) { $count++;
   if (1 == $count) { echo $tag->name . ''; } } } ?>",
});
</script>
<!-- MixPanel End -->

To break down these pieces further, first we have the start and end of the script:

<script type="text/javascript">  //this opens the script
//and
</script>  //closes it.

Everything contained between these two elements defines how MixPanel’s (or any other) script will work.

The first element in the above script is this one:

mixpanel.name_tag('<?php global $current_user;
get_currentuserinfo(); echo $current_user->user_login; ?>');

This allows us to capture the username of the person on the page. Of course, this assumes that the user is logged into WordPress – and most will not be. However, for the few that are, generally administrators of the site, you can separate their use from others by including this line of code. The next element actually does a few different things:

mixpanel.track('Viewed Post', {
    "Title":"<?php single_post_title(); ?>",
    "Author":"<?php the_author(); ?>",
    "Tags":"<?php $posttags = get_the_tags(); $count=0;
   if ($posttags) { foreach($posttags as $tag) { $count++;
   if (1 == $count) { echo $tag->name . ''; } } } ?>",
});

This is the basic functionality of MixPanel, which sends data based on a page view to MixPanel for the user to evaluate. In this case, though, we have a few different variables that we’re asking WordPress to fill in for us.

The first piece of information going to MixPanel is just ‘Viewed Post’ which is defined statically. Attached to that event, however, WordPress is going to fill in the blanks, so the code:

“Title”:”<?php single_post_title(); ?>”

Will actually send MixPanel the event that 1) a post was viewed, and 2) the actual name of the post viewed. This gives the user easy access to track how traffic flows through the site.

We can just track this one variable if we want, in which case our full script would look like this:

<!-- MixPanel Start -->
<script type="text/javascript">
mixpanel.track('Viewed Post', {
    "Title":"<?php single_post_title(); ?>",
});
</script>
<!-- MixPanel End -->

Each individual element can be added or removed, keeping in mind the syntax and necessary opening and closing elements – don’t leave open brackets!

And if you’re looking for an easier way to install MixPanel on WordPress, get our plugin on GitHub.

An upcoming article will help you move beyond basic traffic information, and into more usable data through use of a Facebook Connect Login. Site owners that make use of a Facebook Connect login create an opportunity to gain a great deal of insight into the habits and demographics of their users. In regard to MixPanel, this means opportunities to analyze funnels based on various factors, such as age or location.

3 thoughts on “Setting up MixPanel Functionality in WordPress

  1. mranalogy

    If I use your pluggin, do I also need to manually include the .js call to send the event for each page to MixPanel?
    If so, can I put that .js call in the page or do I need to put it in the footer.php

  2. Jan Pickard

    Hi there,

    I just installed the plug as suggested on latest version of wordpress and get the following error. Any idea?

    Parse error: syntax error, unexpected T_STRING in /home/koraksha/public_html/blognew/wp-content/plugins/mixpanel-wordpress-master/mixpanel.php on line 18

Leave a Reply