Status
- This has been fixed on 6.x-4.x-dev (which has no stable release at the time of writing), but will not be fixed on 6.x-3.x due to the risk of conflicts with other modules
- #31 is the latest and many of us are using this in production since late 2011.
- Google Webmaster Tools does complain that the tracking scripts should be placed in HEAD.
- We were able to successfully move the scripts to head and still have knowledge of whether the page was a 404 or 403 by using the theme layer.
- @hass wants additional dev testing before RTBC - please review!
Original Issue
Google recommends putting all the async tracking code in head. I realize we can't do that because there's no easy way in Drupal to put the tracking code in head AND know whether the page was 404 or 403. (I personally think tracking 404s/403s should be an option, but that's a different issue)
However, I don't see any reason not to put the call to ga.js in the head so it loads earlier. In my (admittedly limited) test, this shaved a few tenths of a second off the time it takes to register a page view for a page with a couple of ad calls and widgets on it. You can measure this yourself in Firefox or Chrome by seeing how long it takes to call __utm.gif -- that's how GA registers a page view.
Curious what you think. Let me know if there are any issues with the patch; it's my first time really working with git.
| Comment | File | Size | Author |
|---|---|---|---|
| #103 | google_analytics-6.x-3.x-code-position-patched1.patch | 4.17 KB | gnindl |
| #68 | error.jpg | 203.07 KB | fenda |
| #43 | 1023384-5.png | 266.96 KB | seandunaway |
| #31 | google_analytics-6.x-3.x-1023384-5.patch | 2.74 KB | seandunaway |
| #29 | google_analytics-6.x-3.x-1023384-3.patch | 2.25 KB | seandunaway |
Comments
Comment #1
hass commented#921308: Document that module using asynchronous javascript but not able to correctly load code in header
Comment #2
eli commentedSorry, I've read the related issues, but I don't understand why splitting the code is bad. Could you give me a hint?
I see that you considered the idea already http://drupal.org/node/648284#comment-3025044 but I can't figure out why it wasn't committed.
Comment #3
hass commentedI don't know if this works reliable. If there are no docs if splitting is allowed and possible you need to upgrade to D7.
Comment #4
hass commentedComment #6
hass commentedFixed patch attached.
Comment #8
hass commentedNitpicking test robot :-)
Comment #9
YK85 commentedEverything looks to be working great with the patch.
@hass - where you able to develop the feature any further? Or is this RTBC for its current state?
Comment #10
hass commentedI have no plans to commit this patch to D6 soon as it is not documented anywhere by google if this is supported or not. Therefore - see this patch "as is" or upgrade to D7.
Comment #11
pjeutr commentedIt is needed in the head by google webmaster central to verify website ownership through the Google Analytics tracking code, as stated in this page (yellow warning block).
https://www.google.com/support/webmasters/bin/answer.py?answer=185871&hl=en
Comment #12
EvanDonovan commentedDoes http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html... answer this question?
Comment #13
hass commentedDo you mean Split Snippet? It shows an example that do not help us...
Comment #14
EvanDonovan commentedThe Split Snippet is not the same as what we are talking about here?
My apologies; it was hard for me to tell from the patch what was happening.
Comment #15
hass commentedNo, flip bottom/header...
Comment #16
seandunaway commentedGoogle Webmaster Tools just recently started complaining about my tracking code not being in also.
Comment #17
seandunaway commentedIs the only thing that requires changing...
from:
to:
?
Comment #18
wiifmIs #17 correct? Is this all that is needed to be done? Was there any reason why the code is currently hard coded to be in the footer?
According to Google:
http://www.google.com/support/analytics/bin/answer.py?answer=74932
Comment #19
Anonymous (not verified) commentedI get a white screen when I apply #17
Comment #20
seandunaway commentedHere is a patch which nixes hook_footer and instead uses hook_init to place the js in the header.
I did a quick run though but with so many options in google_analytics this will need some proper testing.
Comment #22
seandunaway commentedHmm, seems drupal_get_headers() doesn't catch the 404 header until after hook_init().
Comment #23
seandunaway commentedMaybe we can move this to preprocess_page()?
Attached.
Comment #24
seandunaway commentedComment #26
seandunaway commented#23: google_analytics-6.x-3.x-1023384-2.patch queued for re-testing.
Comment #28
seandunaway commentedHmm, so I think hook_init() is out because apparently that isn't run sometimes when serving cached content to anonymous users and also it's too early for 404 information in drupal_set_headers().
hook_preprocess_page() is out because apparently its too late in the execution order to use drupal_add_js().
Gonna try a custom preprocess function next.
Comment #29
seandunaway commentedOkay, this one should pass.
Comment #30
seandunaway commentedComment #31
seandunaway commentedCleaned up and added comments.
Comment #32
izmeez commentedsubscribing
Comment #33
tfo commented+1
Comment #34
fenda commentedsubscribing
what's the roadmap on this? we need to load this in head!
Comment #35
seandunaway commentedI've been running it fine. I think it's ready for commit.
Comment #36
hass commentedThere is NO need for this patch. Roadmap is WON'T FIX for D6.
Comment #37
fenda commented@hass - Why is this? Sorry I can't figure out the reason behind not upgrade. Google recommends putting the asynchronous code in the head for optimal tracking: http://www.google.com/support/analytics/bin/answer.py?answer=174090
Comment #38
hass commentedThere is no technical requirement to move code to head. It's a nice to have and it's where it is for technical reasons. The patch would break other modules integrating with ga. I will not risk compatibility for something that is nice to have. Upgrade to D7 if you need the code in head, please. Or use the patch at your own risk, but do not request for any support here.
Comment #39
seandunaway commentedGoogle complains about the code not being in head. This is a bug per Google. I am not experiencing any incompatibility. Please tell me where and I will work to correctly integrate.
Comment #40
izmeez commentedSo, if i understand correctly,
the patch in
#5#31 is reportedly working,there is concern that if this is committed it may cause conflicts with other modules,
therefore the recommendation is the patch should be used at your own risk.
Does anyone know of modules that have issue with this patch?
I can run some tests on the patch and report my findings.
Thanks.
Comment #41
seandunaway commentedizmeez, Try #31, its the latest.
Comment #42
izmeez commentedYes, I did mean the one in #31, that's the one i'm testing right now.
Patch applies fine to google-analytics-6.x-3.3
The script CDATA still appears at the bottom of the /body
And webmaster tools is failing to verify the site, indicating code needs to be in head.
Flushing caches does not help.
Am I missing something else?
Comment #43
seandunaway commentedIt's moved to head for me... just tested.
Image attached.
Comment #44
seandunaway commentedBy the way, I applied against 6.x-3.x (dev).
Comment #45
seandunaway commented@hass, the latest patch is dramatically different then the one originally posted. It's no longer applying this in hook_footer or hook_init but using a preprocess in the theme layer.
I'm just trying to assess what you meant by break compatibility and now I think you might have been referring to the original copy/paste patch. Have you reviewed the latest?
Thank you for your consideration.
Comment #46
izmeez commented@rump Thanks. I updated to google_analytics-6.x-3.x-dev (2011-06-15), used the patch in #31 and ran update.php
Sure enough the script is moved to the head and google webmaster tools verifies the site without problems. So far there does not appear to be a conflict with other modules on the site and there are quite a few. I will see how it goes and report any problems.
Comment #47
seandunaway commentedMarking rtbc for hass's eyes. @hass, please see #45 and #31.
Comment #48
izmeez commentedI probably should have changed the status to RTBC.
Another advantage of this patch appears to be that Google webmaster tools can then be used fully not just Google analytics.
Comment #49
hass commented@rump: NEVER set Your own patches RTBC. See Ubercart checkout.
Comment #50
hass commentedNice to have is not a bug.
Comment #51
izmeez commented@hass With respect for all the work you have done on this very useful module.
I am changing the status to RTBC as I should have considered doing before.
Fortunately, for sites who wish to make use of this functionality this patch is available.
The larger question is one we have had before in another issue related to this module and that is: "In an open source community such as Drupal, how can individuals make their case to the module maintainer such that they can be heard and such that modules can be improved rather than individuals feeling that the maintainer is simply refusing to add functionality that is useful and possibly needed?
Maybe others can add comments here on the value of adding this "feature" to the 6.x-3.x branch.
Thanks,
Comment #52
hass commentedI'm sorry, but I'm very sure You have not reviewed the code and I do not trust You. You are not a coder if I remember correctly. This code cannot work with the other modules. There is NO NEED for this patch. The module works flawless without this patch.
Comment #53
izmeez commented@hass Thanks, you are right I am not a coder. I can only vouch for testing the patch. Sorry, there is no status just for that, tested. Because of this at first I just provided my report and did not change the status. And I appreciated that @rump was asking you to review the patch.
I wish I had a better understanding of the code to be able to understand your comment
. Hopefully, I will learn more and better understand.
Comment #54
seandunaway commentedI wouldn't say flawlessly considering you have a lot of people watching this thread wanting this functionality and google throws an error. This module is designed to work with Google Analytics and this is a bug report per Google Analytics documentation. Oh well.
Comment #55
seandunaway commentedI'm still hoping you would elaborate on why this code will not be compatible with other modules. I have this on two sites running fine. I'd love to address your concerns but will require more feedback then what you've repeatedly provided. I disagree with your assertion that this patch is not needed. The GA/Google Webmasters errors keep reminding me.
Comment #56
hass commentedPlease keep this case open, at least to stop other from opening new cases.
@rump: other modules rely on hook footer too. They jump in and extend the code. As said you need to test other modules integrating with ga. With 70k installs we cannot change this easily. Previous ga docs said put into footer... some months ago they changed the docs but in past 3 years footer was ok.. Now they recommend the header, but again this does not break anything. It's annoying that webmaster tools are stupid... ~1 year they complained not beeing able to find the code, but docs still said it could be in the footer, too. I don't care that much as long as it works... The tracking works fine and in D7 it's in the header as recommended. In one year 90% will run D7...
Comment #57
seandunaway commentedWhat if this patch is re-rolled with an option on the settings page to move the ga code to head with a note, eg: may break modules which extend ga? The setting would default to the footer. Is it worth considering?
Comment #58
seandunaway commentedI guess another option for your consideration would be a new version branch if compatibility with other modules is a concern.
While I agree that the d7 installs are always increasing... Drupal experienced most of its growth during version 6, with an explosion of contrib and "big" sites/client now running on d6. I think d6 will be unique from other version of Drupal including d7, in that it will have extended life and usage.
I'm always surprised to hear from well known drupal developers (typically in #drupal-contribute) that they are beginning their next project on d6. Yes, even after d7 has been released for almost a year. I myself am beginning work on a new site and have chosen d6 for this particular project.
The gist: Please let's not forget about d6 just yet. However, I do understand the importance of serious testing and not leaving 70k+ installs high and dry.
Comment #59
izmeez commented+1 for the suggestion in comment #57 to add option to settings page.
Comment #60
fenda commented+1 for the suggestion in comment #57 to add option to settings page.
By default we can stick with the current implementation and then we can also keep everyone in this issue happy by letting them decide if they want to move the code to the head.
@hass - would this be acceptable?
Comment #61
EvanDonovan commentedI think moving to the head via a setting on the settings page would be a good solution, as long as people were aware of the potential compatibility issues.
Comment #62
fenda commentedbump :(
Comment #63
simon.fryer commentedAny update on this?
GA wont validate cross-domain tracking if it isn't in the head for me.
Urgency appreciated as it's a live site that isn't being tracked properly. :(
Comment #64
fenda commentedI tried the patch in #31 but my site isn't working with Google webmaster tools.. Odd..
Comment #65
seandunaway commented@drupaljoe, is the tracking script in the head? which checkout did you apply the patch against? i think @izmeez ran into a similar issue around #42 with the solution in #46. or, perhaps this needs a re-roll if there have been any changes to dev. please advise.
Comment #66
fenda commented@rump: Yeah, the script is in the head and I just checked out 6.x-3.x and applied the patch again but still webmaster tools doesn't let me in.
This is the site, you can see the script in the head:http://www.cyac.jp/
Comment #67
seandunaway commentedHmm, weird! Can you provide any details or screenshots with Webmaster Tools regarding any errors?
If it's just not verifying, does it verify properly if you paste the code into the template, just as a test?
Comment #68
fenda commentedScreenshot attached for the error.
Comment #69
seandunaway commented@drupaljoe, the main difference in your code that i'm seeing is you're caching a local copy of ga.js. can you try disabling that feature to use the script hosted by google while we troubleshoot?
additionally, see if removing the cross domain tracking resolves it.
the only difference after that is the cdata comment craziness that might be throwing off webmaster tools.
Comment #70
seandunaway commented@simon.fryer re: #63,
are you having the same issue with webmaster tools not verifying when using cross domain tracking? did you apply #31 to head?
Comment #71
izmeez commented@rump Yes, my problems were resolved as described in comment #46
Comment #72
agoel@axelerant.com commented#31: google_analytics-6.x-3.x-1023384-5.patch queued for re-testing.
Comment #73
yingtho commented#20: google_analytics-6.x-3.x-1023384-1.patch queued for re-testing.
Comment #74
seandunaway commentedFor those just in, #31 is the latest and still passes.
Comment #75
Alex Andrascu commented@hass...one can never stress enough but you mate...you are quite special. Is the patch going to break your own custom modules or why is it that you deny this harmless patch so badly ?
Comment #76
Alex Andrascu commented@rump can you please explain me why have you removed this check:
and why have you replaced
with
Thanks
Comment #77
seandunaway commentedHey Alex, please follow the thread as I documented each step that was necessary to get this to work properly, specifically around #28.
That check was in the code twice, and so now it is only required once. It is still there.
Also regarding the custom preprocess function... for one, it just makes more sense I believe, but more importantly:
Mainly, the 404 tests were breaking.
Thanks,
Comment #78
Alex Andrascu commentedHi rump,
Thanks alot for explaining. It makes perfect sense to me.
Comment #79
sammyframson commentedThank you for your efforts on this @rump. I utilize a 3rd party online booking engine with my Drupal site and it has built in cross-domain tracking capability using GA. However, the booking engine will not load on the page unless the GA script loads first - so the OBE wouldn't load on any page which rendered my booking engine un-usable without your patch. Moving the script to the head has been very helpful and useful and it was certainly required for my implementation.
Thx.
Comment #80
tribe_of_dan commented+1 for the suggestion in comment #57 to add option to settings page.
A list of which modules may be incompatible with this feature would also be desirable if this is added in. If its only Ubercart, then I would happily use this feature as I'm not using that module.
Comment #81
rwohlebUgh, coming back to a D6 project from a D7 project, I'm really missing the scope option. I could have sworn it used to be a D6 option back in the day, but maybe I'm on crack. There is some commented code in googleanalytics.admin.inc for setting the 'googleanalytics_js_scope' variable (as of 6.x-3.3), so...?
+1 for giving people the option in the 'advanced' section with the caveat that if 'you break it you buy it' in other modules.
Comment #82
rjbrown99 commentedSeems like additional effort is required with this to add settings. I just started following, but I'm going to be bold and rotate the status back to needs work.
And count me in as another +1 to add it to the status page with a compatibility warning to scare away folks who may not have the coding skills to debug if/when it breaks something elsewhere.
Comment #83
seandunaway commentedrjbrown99, we're waiting for word from @hass if he will even consider the ui config options. as of right now, i don't think he'll consider this in any form or fashion. he did say he wants to keep this open to catch new comers. i don't think we should put "work" into it until the patch, issue, compatibility, gets some direction from him.
compatibility warning? i think we should first find the incompatibilities. :) it seems several of us are running this patch in production, without issues. i'd like to keep this as need review in hope of even more people (developers) incorporate this patch and show there aren't any issues (or fix them) and mark as RTBC.
Comment #84
mikew3 commentedIf you put the tracking code in the footer, then some analytics features won't work, such as cross domain linking.
For example, see the yellow box here:
http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55532
So it seems pretty important that we have the code in the HEAD block. Would it be better to just paste the script into the theme template and avoid using this module altogether?
(I sent hass an email but I wanted to post here too)
Comment #85
betz commentedany news on this?
Comment #86
seandunaway commented@betz,
Latest is #31 which apparently a few of us are using successfully in production.
@hass would still like for it to have proper review and his concern seems to be about possible conflicts with other modules? I'm not sure which he means.
Comment #87
betz commentedyes, i am also running it on production, but i want this to move on, as i dont want to patch forever.
On drupal 5 and 7 you can choose to put this in the head, why this problem for 6?
Comment #88
buddaI've tested the #31 patch on a clean D6 install and confirmed the jscript is appearing in the right place.
With so many people having this already in production for several months it can be considered stable enough for inclusion in the dev branch for now.
I attempted to search out some Drupal modules which would be affected by this change, according to the earlier comments, but haven't found anything obvious. Open a new issue if one is found.
Comment #89
hass commentedRollback required
Comment #90
hass commented@budda: Don't commit this patch to 3.x branch, please. The "testers" have mostly; if not all - confirmed they are end users only and not developers. This case has a status of "won't fix" for compatibility reasons with other modules and is only kept open to *stop* others from creating duplicates.
This patch adds several troubles and breaks existing features as I know. It has never been tested with Ubercart and I'm not sure about tracking of 403/404 pages. This patch will also break all customization's that people made by using hook_footer() to customize the tracking code. Additional to this issue it is not required at all to keep the tracking up an running properly. The code was correct in past and is still correct. Google cannot deprecate their API until D6 goes end of live. This patch is only a nice to have and all who like to patch can do this at own risk, but it's not ok to risk and support the other 120.000 D6 installations just for a few people who think they must follow a nit-picking dumb Google validation result from today that passed in past. At the time when you left the project the installation base was 1-2% of today. The impact of bugs is therefore extreme and cannot compared to the many complains from past. I had several complains when I marked 3.x as default as the integration's have all been broken. This is a TOP10 module, don't risk it's reputation, please.
I'm open minded to get #231451: Add hook to alter data before sending it to browser done to make hook_footer() integration clean. Until this task is completed and this patch does not work as before this is a no-go for this heavy used project. We can consider creating a new 4.x version branch with this patch, but only if Ubercart and others can integrate and are fully tested and documented. I had no time to review and/or test the last patch and it is much more important to work on the critical issues in this queue than these useless ones.
Comment #90.0
hass commentedUpdated issue summary.
Comment #91
budda@hass your lack of response to the questions about what other modules are to be affected, and no other information as to what you personally know could be broken hasn't been transparent at all. I couldn't find any evidence of modules which would be affected by this change from searching either.
So the change was committed to the 3.x dev, maintainers of other modules have a chance to resolve any integration hacks they might be going to the js code.
The same sort of knock on effects happened with modules like Webform which suddenly changed things and broken 3rd party modules assuming how it works.
Please list examples of the modules which would also need modifying to be compatible with the change to head for ga.js rather than wasting your time repeating the same claim.
This is not a useless issue either. Aside from the posters in this issue we've got clients who have temporarily removed the GA drupal module in favour of hard coding the GA script snippet in to their page tpl to move it to the head to ensure that pages are tracked properly even if the full page assets are never loaded and the visitor clicks back. Ensuring bounce rate is correctly tracked, and improving Googles scoring of pages for clients which are more obsessed about SEO on-page performance and loading speeds.
Maybe a 4.x branch is the solution then to avoid problems with legacy modules hacking in to code.
Comment #92
sebsebseb123 commentedJust so people know... the dev version WAS updated so that the GA code would appear in
Thanks @budda
:)
BUT, the very next day, the dev code was reverted back to putting the GA code at the bottom :(
Sorry @hass but, I'm also having trouble understanding why you're so reluctant to let this happen.
Certain features of GA can only be used with the Async GA code in the
Anyway, the #31 patch no longer works with the latest dev. So, for anyone trying to get this to work... the best option is to get a git clone of it. (Instructions here: http://drupal.org/node/49388/git-instructions/6.x-3.x). Then do a "git log" to see when the last time issue #1023384 was fixed... and do a "git reset --hard #hash". Currently the hash is: 45891c
Hope this helps.
Comment #93
izmeez commented+1 for a 4.x branch to resolve this impasse
Comment #94
hass commentedComment #95
hass commentedComment #96
izmeez commented@hass Thank you for creating the 6.x-4.x-dev branch and the release which can be found at http://drupal.org/node/49388/release and for the comments with the release as to what may be broken.
In consideration of comment #92 I am wondering if the status should be "needs review"? I will do my own testing and provide comments.
Thanks,
Izzy
Comment #98
Alex Andrascu commented@hass " The "testers" have mostly; if not all - confirmed they are end users only and not developers "
I think you might want to speak for yourself on this kind of matters. I haven't confirmed anything with you.
I've been testing it with Ubercart and it doesn't throw any issues.
Thanks.
Alex
Comment #99
hass commentedGoogle fixed their validator for footer and have broken the head validator. #1788586: Google analytics Javascript is not always added at the very end of the head section. So you guys can go back to 6.x-3.x and we can discontinue 4.x.
Comment #100
tribe_of_dan commentedThank God for that. Thanks hass.
Comment #101
seandunaway commented@hass,
If 4.x is discontinued and the only option is to have the ga tracking code in the footer, doesn't that mean that we can't track events and use many of the critical ga features?
Comment #102
hass commentedNope
Comment #103
gnindl commentedI don't know if it's still an issue, but I created a patch for 3.3. It lets you configure the position of the GA code: either in the header or footer.
Comment #105
hass commentedComment #106
gnugetThis issue is marked as fixed but here:
http://drupalcode.org/project/google_analytics.git/commit/253e4572928f12...
Seems to this commit was reverted. So this is still a bug.
Comment #107
gnugetAhh, seems my mistake, in the branch 6.x-4-dev this bug is fixed but in hte 6.x-3.x the commit was reverted.
After to do a diff, seems to the unique diference between this two branches is this particular patch.
Who is the good one? i should use the branch 6.x-4? the 6.x-4 will become the new branch where all the work for this version will be added?
Comment #108
hass commentedComment #109
tribe_of_dan commentedI think what Haas was trying to say was... "6.x-4-dev is no longer needed."
Comment #109.0
tribe_of_dan commentedAdded fixed message.
Comment #109.1
ianthomas_ukRemoving "fixed" note from issue summary, as the fix was reverted the following day
Comment #110
UMG commentedI have recently been trouble shooting some GA issues on my site, and a consultant pointed out that the code is not the head. I've read this thread, and I'm genuinely curious as to why so many think this is not an important distinction, since from what I've read, it does improve tracking.
What is the downside to putting the code in the head, as recommended?
Comment #111
escoles commentedThe code has traditionally been put near the bottom of the page to improve loading performance.
It should now be a moot point, as Google Analytics tracking will now not work reliably and completely if the code's not in the head container.
Comment #111.0
escoles commentedAdd note about 6.x-4.x branch and that this won't be fixed on 6.x-3.x
Comment #112
hass commentedThis commit introduced bug #2335391: Chameleon theme fails to add JS code to page.