• Tracking Mail-To Clicks with Piwik Analytics

    by  • Last modified:February 19, 2013 • Piwik • 1 Comment

    mailto tracking piwik analytics

    Custom Variables to Record Actions
    Custom Variables as we have learnt are very flexible. I wanted to share another cool way you could use events: to track an action taken on your website. In this case, we are going to learn below how to use Custom Variables to track clicks for a Mailto link. As this was disabled, I figured I would add here a way to get the tracking Mailto automatically.

    Using jQuery
    The first step to tracking a Mailto action is to include the jQuery Library. If you are unsure if your page/website has it included, just click view source and look for the word “jquery.js” or “jquery.min.js” . If you do not have it, it’s easy to add. Google actually hosts a version of jQuery, so you can include it easily with the below markup in the <head></head> of your page.

    <!-- Call to jQuery-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <!--end of jQuery call-->
    

    Ensure You have Piwik Tracking
    It goes without saying oyu need Piwik installed, and you want to ensure you have placed your Piwik Analytics tracking code on your site. It should be placed just before </body> tag on your site.

    Now for the HTML

    You need to have a mailto link to track. Here is the basic HTML for an email link

    This is an email link:<a href="mailto:youremail@yourdomain.com?Subject=Test">Send Mail</a>
    

    Please note if your subject spaces, replace them with %20 to ensure that it would be rendered properly.

    Now comes the magic that makes this work and sends the custom variable to your Piwik Analytics install:

    <!--Track mailto-->
    <script>
    jQuery(document).ready(function($) {
        $('a[href^="mailto"]').click(function(){
    	piwikTracker.setCustomVariable(1, "Share By Email", location.href, "page");  piwikTracker.trackPageView(); 
        });
    });
    </script>
    <!--End Track mailto-->
    

    The above code will record the page from which the Mailto link was clicked. If you want the email subject, the code would look like below:

    {...}
    piwikTracker.setCustomVariable(1, "Share By Email", this.href.replace(/^mailto:/i, ''), "page");  piwikTracker.trackPageView();
    {...}
    

    If you want to have the email subject and the page location, below is how you would combine the two:

    {...}
    piwikTracker.setCustomVariable(1, "Share By Email",  'Email='+this.href.replace(/^mailto:/i,'') + ' /Page = ' +  location.href, "page");  piwikTracker.trackPageView();
    {...}
    

    To get the above the final code would look like this:

    <!--Track mailto-->
    <script>
    jQuery(document).ready(function($) {
        $('a[href^="mailto"]').click(function(){
    	piwikTracker.setCustomVariable(1, "Share By Email",  'Email='+this.href.replace(/^mailto:/i,'') + ' /Page = ' +  location.href, "page");  piwikTracker.trackPageView();
        });
    });
    </script>
    <!--End Track mailto-->
    

    So there you have it. Now you have an easy way to track your mailto links in Piwik Analytics!

    About

    About The Author: Adrian has over 12 years experience in Digital Marketing and Analytics. He currently works as a Marketing Manager at Vanilla Forums, a modern forum software platform that allows clients to connect and engage their communities and customers. Adrian has extensive experience with Google and Piwik Analytics. He lives and works in Montreal.