Skip to content

Instantly share code, notes, and snippets.

@ciaranmahoney
Last active March 24, 2017 15:12
Show Gist options
  • Save ciaranmahoney/ded4c740ecf69f291ba0 to your computer and use it in GitHub Desktop.
Save ciaranmahoney/ded4c740ecf69f291ba0 to your computer and use it in GitHub Desktop.
This gist inserts MixPanel link click and pageview tracking scripts into a WordPress blog. It is intended to be included in footer.php so it can track pageview and clicks for all pages. In order for this to work, you must have a Mixpanel account and have Mixpanel tags setup on your website.
<!-- MixPanel Link click tracking - track clicks for all links -->
<script type="text/javascript">// <![CDATA[
mixpanel.track_links("a", "Link Click", {
"referrer": document.referrer
});
// ]]>
</script>
<!-- MixPanel Pageview tracking -->
<?php if( is_page()) { // Sets page type to Page for all pages ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Page'
});
// ]]>
</script>
<?php } elseif( is_single()) { // Sets page type to Post for all single posts ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Post'
});
// ]]>
</script>
<?php } elseif( is_home()) { // Sets page type to Home for home page ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Home'
});
// ]]>
</script>
<?php } elseif( is_category()) { // Sets page type to Category Archive for any category archive page ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Category Archive'
});
// ]]>
</script>
<?php } elseif( is_tag()) { // Sets page type to Tag Archive for any tag archive page ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Tag Archive'
});
// ]]>
</script>
<?php } else { // Sets page type to Other for other pages not mentioned above ?>
<script type="text/javascript">// <![CDATA[
mixpanel.track('Page View', {
'Page Title' : document.title,'URL' : window.location.pathname,'Page Type':'Other'
});
// ]]>
</script>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment