Google have just anonced new Enhanced Link Attribution feature http://analytics.blogspot.ru/2012/11/announcing-enhanced-link-attributio...

The addition js code should be posted before _gaq.push(['_setAccount', 'UA-XXXXXX-Y']); what doesn't allow in GA module now. Documentation http://support.google.com/analytics/bin/answer.py?hl=en&answer=2558867

I think it should be a checkbox to enable this, not a textarea to posting js code.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

hass’s picture

Status: Active » Closed (won't fix)

This looks not functional in Drupal. :-(

I cannot find any link in my D7 that has an ID defined nor any useful element with an ID 3 levels up. Without an ID defined the feature is pointless and does not work at all. This is a no-brainer feature to me as it is not common to add an ID to a link. We have been encouraged over years to use css classes only and leave the IDs behind in every element for flexibility reasons.

plazik’s picture

I think it doesn't depend on Drupal.
If you want to use this feature you can add ID to your link.

hass’s picture

How?

plazik’s picture

For example: in theme, with Menu attributes module, in a text editor etc.

jlea9378’s picture

So how can we add the code to enable this feature?

owen barton’s picture

Status: Closed (won't fix) » Active

From my reading of the docs it only needs "an element" with an ID within 3 levels of DOM parent levels of the clicked element, which (unlike "A" tag IDs) I think is quite often to be the found on typical Drupal sites/links, for example blocks, nodes, views often include div IDs etc.

The example at https://support.google.com/analytics/bin/answer.py?hl=en&utm_id=ad&answe... uses an "a" tag as the example, but the text says "Checks the ID of the click-target element, and if not found, climbs the DOM up to 3 levels to find an element with an ID;" - this makes sense to me, since part of the purpose of this is to track "clickable things" other than just "a" tags.

Reopening to take another look at this.

hass’s picture

We can add it, but I still only have IDs on node forms. This is in my case 5-6 levels or more away from my links. This may be theme related partly, but by default this feature need to be disabled and we need to add a clear message that IDs are not very common.

plazik’s picture

Blocks have IDs by default.

I think it will be useful if someone wants to track information only for some links but not for all. For example it might be Sign up button in header and button before comments. In this case theme developer should adds IDs by hands.

wiifm’s picture

Status: Active » Needs review
StatusFileSize
new1.67 KB

Here is an initial patch for 7.x-1.x to support this new functionality. Would be keen for a few people to test this, and see a) if it works, b) does it break anything.

I believe this option should be disabled by default, as it does require a user to manually change something in their GA profile in order to see the statistics. This should maybe be in the description of the checkbox.

owen barton’s picture

Added a description to the form noting that the feature needs to be enabled in GA. Didn't test actual tracking though.

I also added a preprocess function which will add sequential numeric IDs to all links, but it occurred to me that is probably a bad approach, since the IDs will change over time and GA may start tracking different links are the same. Rather, if we do add an ID I think it would need to incorporate the "right amount" of context in the ID, which I think is a hard problem. My suggestion would be to leave out this chunk and we can see how this performs using block/node IDs.

owen barton’s picture

StatusFileSize
new2.55 KB
hass’s picture

Status: Needs review » Needs work

We are never using l() in translatable strings.

kscheirer’s picture

I think this feature is definitely worth adding. @hass your site may not easily make use of this feature, but that doesn't mean someone else's Drupal site can't.

+++ b/googleanalytics.admin.incundefined
@@ -188,6 +188,12 @@ function googleanalytics_admin_settings_form($form_state) {
+    '#default_value' => variable_get('googleanalytics_tracklinkid', 0),

in D7 you don't need to provide a default for variable gets anymore, the default will be NULL.

+++ b/googleanalytics.moduleundefined
@@ -228,6 +228,14 @@ function googleanalytics_page_alter(&$page) {
+    if (variable_get('googleanalytics_tracklinkid', 0) === 1) {

then you can simplify this to just if (variable_get('googleanalytics_tracklinkid')) { ...

+++ b/googleanalytics.moduleundefined
@@ -416,6 +424,20 @@ function googleanalytics_preprocess_search_results(&$variables) {
+ * Implements hook_preprocess_link().
+ *
+ * If "Enhanced Link Attribution" is enabled, adds a unique ID to links if one
+ * is not already present.
+ */
+function googleanalytics_preprocess_link(&$variables) {
+  static $count = 0;
+  if (variable_get('googleanalytics_tracklinkid', 0) === 1 && empty($variables['options']['attributes']['id'])) {
+    $variables['options']['attributes']['id'] = 'link' . $count;
+    $count++;
+  }
+}

I don't like this approach at all.

Lets leave the site's html alone, enabling this feature does not require this. If you notice your additional link information is not sufficient, we can put this in an example in README.TXT or something.

But we shouldn't assume this much about your site's html.

duntuk’s picture

Does this do the following?:

Track which specific links are clicked on which specific page.

I'm asking because this seems like a basic link tracking need.

For instance, say you have the same 'buy now' button on every page. If you have multiple products, you would want to know from what page that 'buy now' button was clicked from.

kscheirer’s picture

These patches implement google analytics' enhanced link tracking - which does what you're asking. So yes!

duntuk’s picture

Cool! Thanks kscheirer.

Anonymous’s picture

This feature would be very welcome. I see no good reason not to at least include it as an option. I've already got our theme adding unique IDs to links or their immediate parents (at least to the ones we want to track, which are mostly menu links so it was incredibly easy). I just need the analytics code to be right now. If I can't get that through the module, I'll just have to strip the module out of our system and include the analytics code myself in the theme.

hass’s picture

Status: Needs work » Needs review
StatusFileSize
new2.63 KB

I'm also not sure if I like the hook_preprocess_link() and removed it therefore. I think we can commit this for now.

hass’s picture

Minor changes in capitalization.

hass’s picture

Status: Needs review » Needs work

Before committing this I'd like to optimize the setting description. We should explain what this checkbox does and not just telling people they need to enable it in their Google Accounts. This is not useful to understand what they get.

Please make a suggestion.

kscheirer’s picture

Status: Needs work » Reviewed & tested by the community

Perhaps add a link to google's docs on what the ELA feature does?

The patch is rtbc from me - again you don't need to give a 2nd parameter when doing a variable_get() anymore, but that's a pretty minor quibble.

hass’s picture

Status: Reviewed & tested by the community » Needs work

As said, I'm fine with committing this once the "description" really explains what the checkbox does. The current text don't help inexperienced users.

hass’s picture

There seems no upgrade path to Universal Analytics or has someone an idea how it looks like?

soundstripe’s picture

Status: Needs work » Needs review
StatusFileSize
new2.81 KB

I edited the description. Didn't test the patch, but as comment 22 said he was fine with committing with a better description. This good enough?

Status: Needs review » Needs work

The last submitted patch, google_analytics-enhanced_link_attribution-1833578-24.patch, failed testing.

kevishie’s picture

Status: Needs work » Needs review
StatusFileSize
new2.71 KB

Fixed patch from #24

loparr’s picture

Is there same patch for drupal 6? thank you

edit


Now, I am not sure if that is a good way but I simply took the needed code and put it inside the googleanalytics.module like so

$script = 'var _gaq = _gaq || [];';
$script .= 'var pluginUrl = "//www.google-analytics.com/plugins/ga/inpage_linkid.js";';
$script .= '_gaq.push(["_require", "inpage_linkid", pluginUrl]);';

The last two lines are needed according google tutrial https://support.google.com/analytics/answer/2558867?hl=en&utm_id=ad

After clearing cache, the code looks ok, no script errors on page. GA works as usual but Inpage analytics, where this code should take an effect won't start. I get a message - problem loading. We could not find what is wrong with your setup. Please ty later. What could be wrong?

jlea9378’s picture

Seems to work fine. I patched 7.x-1.4 with patch in #26. No errors or anything.

Can we mark this RTBC and get it committed?

neochief’s picture

Issue summary: View changes
StatusFileSize
new1.94 KB

Here's the same changes for 2.x branch (using new analytics code).

neochief’s picture

StatusFileSize
new2.5 KB

Oops, missed the changes in install file. Here's the correct patch for 7.x-2.x branch.

The last submitted patch, 29: enchanced-link-attribution-7.x-2.x-1833578.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 30: enchanced-link-attribution-7.x-2.x-1833578.patch, failed testing.

hass’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Needs work » Needs review
hass’s picture

hyperglide’s picture

hyperglide’s picture

Has anyone had any luck testing this?

  • Commit a58bdc1 on 8.x-2.x by hass:
    Issue #1833578: Add support Enhanced Link Attribution
    

  • Commit e33f56a on 7.x-2.x authored by neochief, committed by hass:
    Issue #1833578: Add support Enhanced Link Attribution
    

  • Commit a676496 on 7.x-1.x authored by kevishie, committed by hass:
    Issue #1833578: Add support Enhanced Link Attribution
    
hass’s picture

Version: 7.x-2.x-dev » 8.x-2.x-dev
Status: Needs review » Fixed

Thanks to all who helped getting this done.

I committed it to all 7 and 8 branches. No backport to 6.x has been done as it is near end of life. If you need this upgrade Drupal Core, please.

hass’s picture

  • Commit dfc8a10 on 8.x-2.x by hass:
    Issue #1833578: Missed to save linkid setting in config
    

Status: Fixed » Closed (fixed)

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

andyg5000 made their first commit to this issue’s fork.