Adding Google Analytics to a Drupal site

26 Jan

Funny thing happened within the last few days, I was asked by three people, "Hey Mike!". "Yes". "How can I add Google Analytics to my [Drupal] site?". My reply... "would you like fries with that?"... just kidding. But that's a decent amount of requests so I think it warrants a quick bloggy blog.

What came to mind? 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 honestly that's not how I did it, maybe it's just confusing the way the instructions are written, maybe new Drupal users (those that asked) don't understand the concept or have the ability to install a module, or whatever, doesn't matter.

For this, installing Google Analytics on your site, I don't think you really need a module anyway's... sorry, no offense budda.

Anyway, I have two or three answers but will provide the easiest for right now, and maybe I'll come back with others later assuming how much crap I get for this post.

Answer One - 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 blow 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

Answer One - 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 in Firefox, ehm, preferred browser.
  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!

But Note! If you're thinking cool, now I'll go to my site and clicking all around on like a crazy person, then login to GA and see a bunch of data, you won't! Google Analytics takes about a day or so to start collecting data. Be patient... hopefully, if your site gets enough traffic, you'll be overwhelmed by the amount of information you just signed up for. If not, still a cool thing to learn how to do.

As always, hope it helped and comment if you get stuck or have a question.

4 Responses to “Adding Google Analytics to a Drupal site”

  1. Guest April 27, 2010 at 3:22 am #

    What is this? "Paste the code snippet that Google gave you into the Block Body." I don't gave from the Google code snippet.

  2. Guest January 18, 2010 at 7:46 pm #

    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

  3. Guest November 24, 2009 at 2:22 pm #

    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.

  4. Guest August 25, 2009 at 12:14 pm #

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

Leave a Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options