Allow custom JS additions in header

agentrickard - May 13, 2009 - 16:30
Project:Google Analytics
Version:6.x-2.2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:by design
Description

As I read the following e-commerce tracking information from Google, it is necessary to put the GA code in the header (or at least, above the links in question). But adding the custom JS code forces that element of the script to be added to the page footer.

Is this a design flaw, or am I misunderstanding something?

1. Add the following lines to your tracking code on both your store site and your shopping cart pages. This code must occur above the code in Step 2.

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();

2. Change the links from the main site to the secure site to use _link as follows. Please note that your analytics tracking code and calls to _gat._getTracker (shown in Step 1) must be placed on the page above the call to _link. If your current links look like:


Purchase Now

change them to:

>a href=" https://www.securecart.com/?store=parameters" onclick="pageTracker._link(this.href); return false;"<Purchase Now>/a<

The code above provides links for users with or without JavaScript enabled. It's important to note that apostrophes need to be escaped with a backslash where they appear in the link or link text.

Important: if your pages include a call to _trackPageview(), _link(), _trackTrans(), or _linkByPost(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening tag and the JavaScript call.

In the above case, the lines:

    pageTracker._setDomainName("none");
    pageTracker._setAllowLinker(true);

Are set to be Custom JavaScript "Code snippet (before):" in the module settings.

#1

agentrickard - May 14, 2009 - 16:11

Bah. Messed up the GA code. Stupid <

1. Add the following lines to your tracking code on both your store site and your shopping cart pages. This code must occur above the code in Step 2.

    <script type="text/javascript">
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    </script>
    <script type="text/javascript">
    var pageTracker = _gat._getTracker("UA-xxxxxx-x");
    pageTracker._setDomainName("none");
    pageTracker._setAllowLinker(true);
    pageTracker._trackPageview();
    </script>

2. Change the links from the main site to the secure site to use _link as follows. Please note that your analytics tracking code and calls to _gat._getTracker (shown in Step 1) must be placed on the page above the call to _link. If your current links look like:

    <a href="https://www.securecart.com/?store=parameters">
      Purchase Now
    </a>

    change them to:

    <a href=" https://www.securecart.com/?store=parameters" onclick="pageTracker._link(this.href); return false;">Purchase Now</a>

The code above provides links for users with or without JavaScript enabled. It's important to note that apostrophes need to be escaped with a backslash where they appear in the link or link text.

Important: if your pages include a call to _trackPageview(), _link(), _trackTrans(), or _linkByPost(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.

#2

hass - May 13, 2009 - 21:25
Status:active» duplicate

What's the problem? I cannot see anything that's not possible today. I don't see any reason why you need to add something to the "header".

Otherwise this sounds like a duplicate of #339235: Add multi/cross domain tracking.

#3

agentrickard - May 14, 2009 - 16:10
Status:duplicate» active

The issues are similar, but I think I am asking for a simpler solution.

As I understand the Google Analytics instructions (and I may be wrong), to use pageTracker._link(this.href), we must first load the JS:

    pageTracker._setDomainName("none");
    pageTracker._setAllowLinker(true);

This is a modification of the standard code provided by GA module.

This can be done by using "Code snippet: before" -- however, that code is only rendered by google_analytics_footer() -- in effect ignoring the advanced "scope" setting of the module. Ignoring the requested scope seems like a module bug to me.

And my understanding is that putting this specific code in the footer is too late, and will cause the desired behavior to fail. The key point is Google's instruction:

Please note that your analytics tracking code and calls to _gat._getTracker (shown in Step 1) must be placed on the page above the call to _link.

So I am not asking (as the other issue seems to be) to add new settings, only to apply the advanced settings we have now. As it stands, when I select "header" as the scope, 2 of the 3 elements are rendered in the header, and one is rendered (incorrectly) in the footer.\

But if the answer is "work on the patch described in #339235: Add multi/cross domain tracking", that's ok by me, too.

#4

hass - May 15, 2009 - 07:12

As I understood this JS code - pageTracker._link(this.href) is only executed if a link is clicked. Therefore we are really easy in initializing all variables in the _gat array/object in footer and after it's init'ed we are fine with the click. I have never used cross domain stuff myself, but this is how GA is normally working. Theoretically it's possible that the remote ga.js file is not yet loaded and you click on a link - than - yes it may fail. You can workaround by setting JS to head in GA settings, but you are on your own with this setting... it's not supported by Google. JS code should be located in footer... or page load will be delayed. I believe some of this documentation is really confusing at google... changing the order have many other side effects and I'm not sure why google doesn't explain this together with the domain linking. It's very easy to say "move it to the top", but miss to tell that this would delay page loading and some other details I cannot remember now.

Until you are not able to prove there is something broken and why I don't think this is a bug or design issue. The module works as Google has designed GA.

#5

agentrickard - May 15, 2009 - 14:11

The bug is that if I say "move JS to header" -- despite warnings to the contrary -- not all JS moves to the header. The custom bits are rendered in the footer, contrary to expectations.

I agree that the instructions are confusing, and I don't have immediate access to GA test feedback, so I can't verify if this approach works or not.

#6

hass - May 16, 2009 - 19:39
Status:active» by design

If we would move all up we are not able add all required things to the function we need. for e.g. search result keywords, profile fields and others... this is caused by the hook system that drupal have... many things are fired later than hook_init, but we need the information. So no chance to change - caused by core design :-)

#7

nosro - September 10, 2009 - 14:44

I am having the same issue of needing to move the "pageTracker._trackPageview();" and associated calls into the header, as per the Google instructions here: http://www.google.com/support/googleanalytics/bin/answer.py?answer=55532.

I see the problem with not having all the data ready at hook_init, so I'm stumped for a solution.

I wonder if I could use a function in the theme template.php to find and extract the page footer $closure bits I need and move them into the page header $scripts? I'm going to try that.

#8

OFF - December 19, 2009 - 22:00

Subscribe

 
 

Drupal is a registered trademark of Dries Buytaert.