Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using Wordpress to present some software for download. The download links are provided via Custom Link entries in a side menu. I would like to enable Google Analytics tracking for these file downloads but I can't see how I can add the required javascript triggers as per the example here: http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55529.

Can anyone offer a way of achieving this?

Edit: By "Custom Link", I mean that the file links are created in a custom menu as custom links in the Appearance->Menus area of WP3. I'm using the Atahualpa theme, if that makes any difference.

Further Edit: I'm trying the plug-in route, which will hopefully automate the process, but another possibility I've found is to use the "Text" widget and simply paste the menu code in there and edit in the javascript trigger on the links. This is obviously more of a pain to maintain. I would add this as a possible answer, but I can't answer my own question, apparently.

Outcome: Thanks for the responses. I've ended up going with the Text widget and just hand-coding the menu for now.

I'm not sure why the answer simply pasting the info from the link I gave in the question is being up-voted as it doesn't address how to do this in Wordpress. A plugin that allows adding onclick handlers to the custom links or similar would be best, but I can't find one and "if you had a unicorn" isn't an answer either. I may come back to the coding suggestions but the creation of useful GA identifiers seems difficult to do automatically, which seems to lead back to effectively writing my own plugin... and surely that's an answer to almost anything?

share|improve this question
    
Could you please specify which code/widget/whatever you use to display Links? –  Rarst Nov 9 '10 at 11:36
    
To echo Rarst's original question, what "custom links feature" are you referring to? Are you trying to track clicks in the blogroll, or in links placed on a post/page? If you're trying to track all clicks, t31os's solution is the way to go. If you're only trying to track specific links on a post/page, mireille raad's solution is the way to go. –  EAMann Nov 9 '10 at 15:22
    
I agree, it would help a great deal for the question to be further clarified, but hopefully we've covered the necessary information between us anyway.. ;) –  t31os Nov 9 '10 at 15:29
    
You probably then look for a plugin that extends the menu with further attributes next to class - like onclick. –  hakre Nov 9 '10 at 18:48
    
This question is related to menu meta-boxes as well: wordpress.stackexchange.com/questions/2770/… –  hakre Nov 9 '10 at 19:56

4 Answers 4

@Eric: I had this same issue tracking my eBook downloads on my vegan lifestyle website.

The problem is I offer a lot of eBooks and adding the link 1-by-1 just isn't practical for me. I Googled around and found Entourage.js:

http://techoctave.com/c7/posts/58-entourage-js-automatic-download-tracking-for-asynchronous-google-analytics

I like it because it has automatic file download tracking. The article has the installation details and download link. But for me, it was as simple as adding the following code snippet before my Google Analytics snippet inside the <head> tag:

<script src="javascripts/entourage.js"></script>

You can replace the "javascripts" directory with the location of your javascript files. Hope this helps someone. I was frustrated until I found this solution. Now things just work!

share|improve this answer
    
You should use wp_enqueue_script() to make it easier to turn it off in certain conditions. –  toscho Nov 12 '12 at 18:49

If you are using a plugin like http://yoast.com/wordpress/google-analytics/ then you can just enable outbound link tracking in the settings. It will append a tracking URL for you so you can segment in analytics.

share|improve this answer
    
Thanks, I've tried setting up the plug-in and will wait to see what the results are. If a plug-in can automatically add tracking to downloads then this seems the best option. –  Eric Jones Nov 9 '10 at 18:30
    
Hmm... I've tried to get the yoast plugin to work for me but I'm failing. I've activated download tracking and also tracking as pageviews so I can have GA goals, set the file types to "exe,zip" and the prefix as "/downloads/". No content accesses have come through on GA and looking at debug in Firebug shows no tracking data being sent. Perhaps the plugin doesn't work for custom links? –  Eric Jones Nov 9 '10 at 23:17

I was hoping you would clarify how you're outputing these links, as per Rarst's comment..
That said, i'm going to assume you're using wp_list_bookmarks to create a list of these links (there aren't many other functions for the purpose).

For wp_list_bookmarks there's only one hook available and that's called(ironically) wp_list_bookmarks, which gives you a string with the output. The following should add the necessary code onto links for you..

add_filter( 'wp_list_bookmarks', 'add_analytics_tracking' ); 
function add_analytics_tracking( $bookmarks_string ) {
    $bookmarks_string = str_replace( 
        '<a href', // Replace this
        '<a onClick="javascript: pageTracker._trackPageview(\'/downloads/map\');" href', // With this
        $bookmarks_string // In this
    );
    return $bookmarks_string;
}

A second option would be to use get_bookmarks (see the example on that codex page), just of course bear in mind that get_bookmarks does little to sanitize the data you're getting back, wp_list_bookmarks will deal with sanitization for you..

Hope that helps in some way... :)

share|improve this answer
    
There's also a filter within the the nav menu function that you can use. Replace wp_list_bookmarks with walker_nav_menu_start_el in the code above and you can use it with your custom menu links. –  goldenapples Nov 9 '10 at 18:35
    
And also... I didn't notice this before... '/downloads/map/' refers to how you want the pageview to show up in your GA account. You should probably replace that with something unique to the file itself, or else you'll be tracking every download in one metric. Just saying. –  goldenapples Nov 10 '10 at 2:12
    
Of course and it's covered on the google page linked above (i didn't feel there was a need to repeat what should have already been gleaned from reading the information). I assumed Eric simply wanted help with the PHP side of things, he's welcome to update that line with his own desired value(s). Thanks for pointing that out all the same, it's always possible small things get overlooked.. –  t31os Nov 10 '10 at 11:05

You basically have to tag the <a> link

Google Analytics provides an easy way to track clicks on links that lead to file downloads.

Because these links do not lead to a page on your site containing the tracking code, you'll need to tag the link itself with the _trackPageview() JavaScript if you would like to track these downloads. This piece of JavaScript assigns a pageview to any click on a link - the pageview is attributed to the filename you specify.

For example, to log every click on a particular link to www.example.com/files/map.pdf as a pageview for /downloads/map you would add the following attribute to the link's tag:

<a href="http://www.example.com/files/map.pdf" onClick="javascript: pageTracker._trackPageview('/downloads/map'); ">

http://www.google.com/support/analytics/bin/answer.py?hl=en&answer=55529

Hope that helps :)

share|improve this answer
    
Indeed, that's the link I gave in my question. I understand what is required as HTML. What I don't know is how to implement that via Wordpress's custom links feature, which provides a place to enter the target URL and the label, not arbitrary HTML. –  Eric Jones Nov 9 '10 at 9:32
    
Hope this works: jQuery('a[href="example.com/files/map.pdf"]).click(function(){pageTra‌​cker._trackPageview('/downloads/map');}); - it's roughly written, you need to inject this on document load with some javascript. The wordpress menu does not support javascript in the backend in core. –  hakre Nov 9 '10 at 18:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.