
Funny thing happened within the last few days, I was asked by three people, how to add Google Analytics to a Drupal site.
What came to mind? Well, maybe you could find this on Drupal.org (yes, you can), maybe they have a module for it (yes, they do).
So they do, but that's not how I did it. Maybe it's just confusing the way the instructions were written or maybe new Drupal users don't understand the concept or have the ability to install a module, or whatever, doesn't matter.
For this, we'll just use a block.
Step One
Before you start, make sure you have the PHP filter enabled in your modules section, otherwise Step 6 won't make any sense.
- Login to your site.
- Click Administer -> Site Building -> Blocks -> Add Block.
- Give the new block a Block Description of Google Analytics.
- Leave the Title blank, no need for it.
- Paste the code snippet that Google gave you into the Block Body.
- Just below the Block Body, click the Input Format, and set it to PHP code (yes, I know you're pasting in Javascript, trust me people)
- Scroll down, click Save Block
Step Two
- Login to your site. *Not assuming you're still logged in from step one
- Click Administer -> Site Building -> Blocks.
- In the list of Blocks you should now see Google Analytics listed and the Region set to None.
- Set the Region to be whatever is closest to the footer, some themes have a left/right/center, or whatever footer... just pick one of those
- Scroll down, click Save Blocks
That's it, you're done.
Test it to make sure it's on the page
- Browse to your site.
- View the source of any page.
- Scroll down in the source and look for the snippet of Google code you pasted into the new block.
- If you see it, you should be all set.
Note! If you're thinking, cool, now I'll go to my site and click all around like crazy and login to GA too see all of your new data, you won't. Google Analytics takes about a day or so to start collecting.
As always, hope it helped and comment if you get stuck or have a question.






Comments (54)
Google Analytics module?
Why not just use the Google Analytics module, like everyone else, instead of building a hacked, non-standard solution? Heard of "the Drupal Way...?" :)
custom pages
hey, it worked!
the only this is, that i only get the directory they're visiting, i never get the actual page ( i would like to though).. is there any way to add this?
Sorry, not sure about that. A
Sorry, not sure about that. A lot has changed on the Google Analytics-side since I wrote the original article.
An alternative approach
This is what I did for a Drupal 7 site with it's own theme MYTHEME:
In your theme folder (sites/all/themes/MYTHEME), create a folder named js if there isn't one already. Create a file called something like google-analytics.js. The name doesn't matter, but the suffix must be .js. Copy the Google Analytics tracking code supplied by Google and paste into this file, leaving out the script tags . The file will look like this:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
where UA-XXXXX-Y is your GA account number.
Now create a file template.php in your theme folder if there isn't one already. Add the following code to this file:
function MYTHEME_preprocess_html() {
drupal_add_js (path_to_theme() . '/js/google-analytics.js');
}
This should add the script to the header of each page. You might have to clear your caches first.
The page I got the following code from seems to have disappeared, otherwise I would credit the author of this tip.
Drupal 7 - Looking Good
I followed the instructions and all is looking good. I see the analytics code on the source for my page. I also used a pretty awesome Chrome extension for testing google web analytics to verify that the js request was looking good.
You can find the Chrome Extension here:
https://chrome.google.com/webstore/detail/jnkmfdileelhofjcijamephohjechhna
When you enable it for a site it logs the data being sent to Google in the browser console. Which you can view by clicking the Wrench/Settings icon. -> Tools -> Javascript Console.
Thanks for this awesome writeup!
You're welcome!
And thanks for the tip on the Chrome extension. When I find some time I'll surely check it out!
adding anyalitcs code to drupal.
worked like a charm although I used Piwik does the same thing in real time. Thank you so very much, had been searching the site forever
Awesome Instructions
Thanks! I added the block, and it was very simple. I see the code added in the footer but it's not exactly at the bottom, as Google suggested. I'll wait a day or so to see if the reporting works. THANK YOU!
Google recommends...
RE: Head tag
Great walk-through; very easy to understand. But how do I insert the code into the head tag?
Thanks!
It works a treat
Thanks for that tip. I have just set it up in Drupal7 and Google says it can see it. I look forward to seeing the results.
We all like treats!
Great to hear... have fun with it!
How to remove Google analytics from a Drupal site
Nice, this way Drupal hides the data leak to Google deep in a js-component making it hard for users to filter out :-<
Say what?
Hi,
Sorry but I'm not sure what your comment is referring to. Would you mind elaborating?
Doesn't work anymore?
Just tried this with Drupal 6 and placing the block in the footer or anywhere else doesn't seem to work. The Google instructions at http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html say that the code has to be before /head and it doesn't look like there's a way to get it to appear within the head via the approach suggested here? Thanks.
Did you wait?
I agree with your comment on the async tracking but you can still place the code in the "footer" with Drupal 6, and it'll work.
Did you install it, verify it, and wait the 24-ish hours before you checked for data?
thanks
thanks buddy. i have done it.
Cool.
Nice, buddy, glad it worked. ;)
Waiting for the result
I Followed the instructions but can not see the code on the source code. I'll wait a few days to see if it is alive...
Alive?!
Hey there... hope it's alive by now!
Thank You!
Thank You very much. I added analytics as per your instructions here. Now the wait begins for tomorrow.
-Shwetha
Good Luck!
I'll be upgrading this to support Drupal 7 too, soon.
Grrrr! Google and Drupal
Can you help? I've followed your instructions and nothing shows. I am looking after a site that I didn't build (and I don't know a lot about Drupal) but I couldn't get the GA module to work...and I can't get your method to work!
Grrr!
Before you start, make sure you have the PHP filter enabled in your modules section...
If I could read it would probably help!
Thanks - looks like it's worked!!
Still not working
Whatever I do I can't get it to work!
Sure I can have a look. Could
Sure I can have a look. Could you send a link to the website you're working on?
You are the best
Finally something I understand, great workaround! Hopefully it will start collecting in a day now!! Made my day :-)))
Did as you instructed
Did as you instructed lastnight, now it is just the waiting game... hope this works for me. Will post back either way. Also thank you very much for sharing this method of using GA with a Drupal site.
Good Luck
Good luck, let me know how it goes. BTW - what version of Drupal are you using?
Removing Yourself From Being A 'Hit'?
Awesome info!
One question, how would you modify it so that you did not count as a 'hit' to your own website?
Cheers
hmmm, not sure
You can configure Google Analytics to ignore "hits/visits" form specific IPs and/or IP ranges...but sorry, not exactly sure where.
Won't work.. :(
Hi there
I was hoping that this would solve it for me, however once I create the block (PHP Filter is on and input format is PHP ) the code just shows as text at the bottom of each page? Did I miss something?
Marco
I am having the same issue
I am having the same issue with the code appearing at the bottom of every page. How is this hidden from the display?
Adding google analytics to drupal site
What does "just blow the block body" mean in step 1 # 6
err, typo
Sorry, should have read "below".
thanks
looks that it will work (waiting 24 hours for data to appear)
Madara
Awsome
Worked like a charm.
Cool
Cool!
Is anyone running Drupal 7 yet? Does this still work?
Great help - best answer
Thanks a lot sir. I searched for this many times, may be months.
I had done almost 75% correct. Input format PHP code and Module PHP Filtering enabling missed. This kind of help is required for a newbie like me. My site is http://vision4life.in - Design/creation/owner is a newbie.
You're welcome!
You're welcome!
Thanks
Thank you very much.
Reply to comment | Mike Hommé
I am Cipto Junaedy,
Its like you learn my thoughts! You seem to understand
a lot approximately this, like you wrote the e-book in it
or something. I feel that you simply can do with some % to force the message house a bit, however other than that, this is excellent blog. A fantastic read. I will definitely be back.
Block Body
What is this? "Paste the code snippet that Google gave you into the Block Body." I don't gave from the Google code snippet.
Thank you
I Unfortunately got this page after I have installed the GA module for Drupal. (you know it's the only result for "add Google Analytics to drupal" searching)
Well I typed just the ID in the module settings page, but not sure if that enough.
I am waiting the 24 hours you pointed above, and if doesn't work, then will try your simple way
Thanx Mice
Thanks!
I just started using Drupal and went to add my analytics code and realized that it wasnt showing up.
I was able to figure out the adding a block and then setting the region to footer but completely missed the php filter settings.
A quick google search and here I am.
Thanks very much for taking the time to add this.
Thanks!
Thanks for the help!
I've done that, we'll see tomorrow or so if it actually works! ^_^
thanks!
Yui
Very Strange, i followed your
Very Strange, i followed your instructions step by step and the only thing i see is the written code i put in the body.
What did I do wrong?
set to plain text
Switch to plain text editor -
switch to plain text editor?
When pasting in the google code, should I paste it into the wysiwyg when in normal text view or when in the "source" view? -or- for another variation, change the input to php and then paste the code into the block body (which then has no wysiwyg interface)?
Plain text, for sure.
Hi, without trying, I would still suggest pasting the Google code in a Plain Text view, not WYSIWYG.