Can I track clicks on a mailto link?

Avatar
  • updated
  • Answered
I'd like to be able to track the number of clicks on a mailto link and wondered if this is possible in WebLog Expert? If so, do you know what code I should add to the page/link to get this to work?
Avatar
Michael
If you wish to get more accurate reports you can do the following:

1. Download file http://www.weblogexpert.com/files/tra..., rename it to mail_tracker.gif and place it to the root folder of your site.

2. Modify mailto links on your site like the following:

<a href="mailto:test@test.com" onclick="(new Image()).src = '/mail_tracker.gif?mail=test@test.com&r=' + Math.random().toString().slice(2, 10);">test@test.com</a>

3. Add a new table in WebLog Expert in Options > Report > Tables. On the second page vhoose "Query parameter value" as main data, click the "Custom..." button to the right of it and enter "mail" (without quotes) as parameter name. Choose "Hits" and "Visitors" columns.

On the next (filter) page add an include "Requested file" filter with value /mail_tracker.gif*

After it you'll be able to get reports on mailto clicks in a separate table.

If you have multiple links to be tracked, it is better to use a script to add the onclick event automatically to all mailto links without need to add it manually. If you use jQuery, you can use the following script:

$(function(){

$('a[href^="mailto:"]').click(function(){
var mail = $.trim($(this).attr('href').substr(7));
(new Image()).src = 'mail_tracker.gif?mail=' + mail + '&r=' + Math.random().toString().slice(2, 10);
});
});
Avatar
allan maclean
Hi Michael,

Many thanks for your very prompt, helpful response. It would be great if you could provide more information.

Thanks
Allan
Avatar
Michael
You need to add some JavaScript code to load a file from your server each time the link is clicked.

For example, you can use the following code:

<a href="mailto:test@test.com" onclick="(new Image()).src = 'http://www.yoursite.com/mailto.html?mail=test@test.com';">test@test.com</a>

You also need to create an empty file mailto.html on your server. After it you can enable the Access Statistics > Pages and Queries report and view statistics on all queries for this file that will include the email clicked.

The only downside is that some duplicate clicks won't be logged as browsers will cache the response (e.g. for /mailto.html?mail=test@test.com). It is possible to avoid the issue by creating a bit more complex script and using custom tables (it requires the Professional or Enterprise edition) - if you wish, I can provide more information on how to do it.