Tracking links using Google Analytics in SharePoint 2010 with Mavention Google Analytics Links Tracking
It’s for analytics
Many customers who use the Microsoft SharePoint 2010 platform for their public-facing website, use Google Analytics for tracking analytics data. One of the things that Google Analytics allows you to track are links. Using custom events you can track which links your visitors use, what can provide you with some additional insight in how your website is used.
To track a click on a link using Google Analytics all you have to do is to add an onclick event to your hyperlink:
<a href="#" onClick="_gaq.push(['_trackEvent', 'Downloads', 'Document', 'My Document.docx']);">Download My Document</a>
Note: You can find more information about event tracking in the Event Tracking Guide
As you can see this approach is straight-forward. Adding an onclick event to a hyperlink isn’t much of a challenge in SharePoint 2010 unless you want to do it in the Rich Text Editor.
Inconvenient tracking links and SharePoint 2010 Publishing HTML fields
One workaround to this challenge would be not to use the Publishing HTML Field Type and have the HTML stored as text in SharePoint. And while this might seem a good idea at first, it has some trade-offs among which losing support for Alternate Access Mappings.
Although the Publishing HTML Field Type does not allow us to use unsafe markup, there is a way to work around it.
Tracking links in content using Google Analytics with Mavention Google Analytics Links Tracking
Mavention Google Analytics Links Tracking is a SharePoint 2010 Sandboxed Solution that allows you to add Google Analytics event tracking information to your links. Since using the onclick attribute within Publishing HTML fields is not allowed, Mavention Google Analytics Links Tracking stores the Google Analytics tracking information as a CSS class which on every page load is transformed into an onclick attribute.
What’s in the box
After installing the Mavention Google Analytics Links Tracking solution the following three Site Collection-scoped Features are installed:
- Mavention Google Analytics Links Tracking – this is the core Feature that installs all scripts required for functioning of the solution and installs the UI for managing Google Analytics event tracking information on hyperlinks.
- Mavention Google Analytics Links Tracking jQuery registration – this Feature registers jQuery upon which Mavention Google Analytics Links Tracking scripts depend. You don’t have to activate this Feature if you’re using jQuery yourself.
- Mavention Google Analytics Links Tracking registration script – this Feature registers the tracking script responsible for transforming the CSS class with analytics information into an onclick attribute. This Feature should be activated unless you have referenced the tracking script (located at: ~SiteCollection/Style Library/Mavention/GALinksTracking/Mavention.GALinksTracking.js) yourself to for example limit the number of requests on your site.
How it works
If you edit a Publishing Page and focus on a hyperlink you should see the new Google Analytics Event Tracking group on the Ribbon in the Links Tools contextual group.
After you enter the tracking data and zoom in on the markup of the hyperlink, you should notice the newly added CSS class prefixed with gat-.
This CSS class contains all the information that is required to build up the Google Analytics Event Tracking code.
After you publish the page you can once again confirm that the CSS class hasn’t been removed. And if you have hooked up everything correctly, next to the gat- CSS class you should see the onclick attribute added to the hyperlink markup.
If you have Google Analytics registered with your page, the next time someone clicks on that links, an analytics event will be registered and you will be able to track the information from your analytics dashboard.
Using Google Analytics you can track clicks on hyperlinks within your site. Unfortunately the Google Analytics Event Tracking tracking code is registered using the inline onclick attribute which is not allowed in Publishing HTML fields. Using the Mavention Google Analytics Links Tracking solution you can store the Google Analytics Event Tracking information with your link without any extra effort.
Download: Mavention Google Analytics Links Tracking (39,1KB, Sandboxed WSP)