Adding Google Analytics to a Drupal site

January 26, 2009 — Mike Hommé
Mike Hommé's picture

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.

  1. Login to your site.
  2. Click Administer -> Site Building -> Blocks -> Add Block.
  3. Give the new block a Block Description of Google Analytics.
  4. Leave the Title blank, no need for it.
  5. Paste the code snippet that Google gave you into the Block Body.
  6. 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)
  7. Scroll down, click Save Block

Step Two

  1. Login to your site. *Not assuming you're still logged in from step one
  2. Click Administer -> Site Building -> Blocks.
  3. In the list of Blocks you should now see Google Analytics listed and the Region set to None.
  4. 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
  5. Scroll down, click Save Blocks

That's it, you're done.

Test it to make sure it's on the page

  1. Browse to your site.
  2. View the source of any page.
  3. Scroll down in the source and look for the snippet of Google code you pasted into the new block.
  4. 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 (42)

Guest's picture

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!

Mike Hommé's picture

Google recommends...

You're welcome. But I've seen lately (as of yesterday) Google is recommending that their Analytic code be placed before the ending HEAD tag. Will update you all with this soon.
Guest's picture

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.

Mike Hommé's picture

We all like treats!

Great to hear... have fun with it!

Guest's picture

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 :-<

Mike Hommé's picture

Say what?

Hi,

Sorry but I'm not sure what your comment is referring to. Would you mind elaborating?

Guest's picture

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.

Mike Hommé's picture

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?

Guest's picture

thanks

thanks buddy. i have done it.

Mike Hommé's picture

Cool.

Nice, buddy, glad it worked. ;)

Guest's picture

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...

Mike Hommé's picture

Alive?!

Hey there... hope it's alive by now!

Guest's picture

Thank You!

Thank You very much. I added analytics as per your instructions here. Now the wait begins for tomorrow.

-Shwetha

Mike Hommé's picture

Good Luck!

I'll be upgrading this to support Drupal 7 too, soon.

Guest's picture

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!

Guest's picture

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!!

Guest's picture

Still not working

Whatever I do I can't get it to work!

Mike Hommé's picture

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?

Guest's picture

You are the best

Finally something I understand, great workaround! Hopefully it will start collecting in a day now!! Made my day :-)))

Guest's picture

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.

Mike Hommé's picture

Good Luck

Good luck, let me know how it goes. BTW - what version of Drupal are you using?

Guest's picture

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

Mike Hommé's picture

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.

Guest's picture

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

Guest's picture

Adding google analytics to drupal site

What does "just blow the block body" mean in step 1 # 6

Mike Hommé's picture

err, typo

Sorry, should have read "below".

Guest's picture

thanks

looks that it will work (waiting 24 hours for data to appear)

Madara

Guest's picture

Awsome

Worked like a charm.

Mike Hommé's picture

Cool

Cool!

Is anyone running Drupal 7 yet? Does this still work?

Guest's picture

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.

Mike Hommé's picture

You're welcome!

You're welcome!

Guest's picture

Thanks

Thank you very much.

Guest's picture

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.

Guest's picture

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

Guest's picture

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.

Guest's picture

Thanks!

Thanks for the help!
I've done that, we'll see tomorrow or so if it actually works! ^_^
thanks!
Yui

Guest's picture

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?

Guest's picture

set to plain text

Switch to plain text editor -

Guest's picture

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)?

Mike Hommé's picture

Plain text, for sure.

Hi, without trying, I would still suggest pasting the Google code in a Plain Text view, not WYSIWYG.

Guest's picture

Thank you!

I followed your instructions for a site that I 'inherited' and they worked like a charm. Thanks for writing such straight forward steps that even a novice like myself can follow.

Unconvinced Drupal-Curve Rider

Mike Hommé's picture

Nice!

Great to hear, you're welcome!