I wanted to use Google Analytics to report on click-thrus to offsite links. Since the links are offsite, they're not automatically captured. The solution that Google recommends is to add an onClick attribute to the anchor tag. However, I felt finding and adding the appropriate javascript was too onerous / unrealistic for our editorial staff; I wanted to make it easier for them to tag offsite links.

Inspired by the gotwo module, I added a filter that would translate a new anchor tag attribute, gatag, into the appropriate onClick='javascript:...' attribute. I also added some admin settings to add an identifying string and optionally add source and / or destination information to every manually tagged link.

For example, our editors could enter the link <a href="http://youtube.com" gatag="entertainment">. The filter might translate that link into <a href="http://youtube.com" onClick="javascript: pageTracker._trackPageview('/tagged/entertainment');">. Alternatively, if the administrator has specified that information about both the source and destination of the link be included in the tag, the same link might also be translated to <a href="http://youtube.com" onClick="javascript: pageTracker._trackPageview('/tagged/entertainment/SOURCE/node/999999/DESTINATION/youtube.com');">. [I'm brand new to google analytics, and don't know much about how to segment data on the reports. If there's a better way to format the tag so it's easier to report on sources and or destinations, please modify the code appropriately.]

Please note that to use the filter, the admin has to a) enable the filter for the Full HTML input format (Filtered HTML strips out javascript), and b) give it a lower weight (higher priority) than the HTML filter (the HTML filter would strip out the gatag attribute). Also, any text that uses the gatag attribute must be entered in Full HTML format, so its usage is likely limited to editorial and other internal staff that have the right to post Full HTML content. [However, the actual translation of the attribute is done in a function that can be passed any text that starts with an anchor tag, e.g. the output of l(). So you might find other contexts in which you can use this extension.]

FYI, we're currently running an old version of this module (googleanalytics.info:; $Id: googleanalytics.info,v 1.2.2.1 2007/01/18 01:16:02 budda Exp $). Although the attached patch is relative to the latest released version, 5.x-1.4, I haven't actually tested it with that version. However, the code is completely new code and doesn't depend on existing functionality in the module, so it should work properly. (It does honor the googleanalytics_legacy_version variable to generate the appropriate function call.)

I hope you find this feature useful enough to incorporate into the module.

[Credits: I did use some of the code I found in the gotwo module as a starting point for this code.]

CommentFileSizeAuthor
google_analytics_gatag.patch7.99 KBprfctns6@gmail.com
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hass’s picture

Status: Needs review » Needs work

Nice idea, but have you seen outbound tracking with the "downloadtracker.js yet? Download tracker is not the best name here... i think this was the intention earlier and have such a function already build in for outbound link tracking. Therefor i think this task might be much simpler and speedier to archive then your filter approach.

Maybe we could add a special and valid CSS class to a link like "gatrack" and filter with jquery if the clicked link have this class set... this will speed up page loading and reduce complexity. Or we add custom filter to GA like track all links starting with /go/* as outbound links.

Aside some comments about your patch:
1. Read and follow http://drupal.org/coding-standards. Current patch does not fulfill this rules.
2. Code produces invalid XHTML code (e.g. onClick is invalid)
3. Code seems not E_ALL save.
4. Uninstall of new variables is missing

vkr11’s picture

subscribe

that0n3guy’s picture

Two questions:

1. Does the google analytics module use downloadtracker.js to automatically track outbound links?

2. can downloadtracker.js be used to track internal links (I have internal links that redirect)? For example, mysite.com/outbound/external-links redirects to www.external-link.com so it isnt currently tracked.

Thanks,

vkr11’s picture

What is downloadtracker.js ?

Thanks,
Victor

hass’s picture

Outbound links that start with http, https and ftp and not matching the file download extension list are already tracked as "/outgoing/*". I think we only need to extend downloadtracker.js with a path filter regex or css class filter or both that could meet all possibilities.

that0n3guy’s picture

There is an External links filter module http://drupal.org/project/elf but I dont know if it does what I want.

I am looking for a way to define what an "external" link is. Is that what you mean by a "filter". So I could have the link localhost/external/taxes (it would be an internal link) but since it has the "/external/" on it, it would be tracked as an external click.

Also, it would be nice to be able to define how it was tracked, so downloads are tracked as "/outgoing/downloads" rather than with the rest of the "outgoing" links.

Thanks

vkr11’s picture

Do you have to do something to track the /outgoing/? Cause I am not seeing any outgoing in my analytics report...

samc’s picture

subscribing

hass’s picture

Status: Needs work » Fixed

I have added 2 lines to the JS code that will to the trick for gotwo.module. It simply searches a regex of /go/* and fires the tracking image to google.

I'm not sure if this is all what the original poster want, but i hope so. Today this regex is not configurable in UI and i expect this is too complex in normal case. So - if there are more modules that have special url's like gotwo have, please tell us so i can add more url's to the regex. We could think about an additional css class or something similar to be more general.

If this does not meet the requested functionality, please reopen.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.