Drupal FAQ: How do I install Google Analytics on a Drupal website?
There are two ways to do this, and I think they're both pretty easy. In the first approach you can just add the Google Analytics code to your Drupal template files, which is basically a copy and paste operation. However, if you don't want to do that, you can also use a Drupal "Google Analytics" module, and I share the link to that module below.
In this article I'll share the steps for the first approach -- adding the Google Analytics code to your Drupal theme pages, specifically your page.tpl.php file. If you don't mind touching your website theme code just a little bit, this is a very easy installation.
Adding Google Analytics to a Drupal theme
In short, all you have to do to set up your Drupal Google Analytics system is:
- Create a Google Analytics account.
- Get the HTML code that Google tells you to insert onto your web pages.
- Open the page.tpl.php file for your Drupal theme, and insert the Google Analytics HTML code just before the "</head>" tag in that page, i.e., copy the code from the Google site and insert it into this Drupal theme page.
- Upload this page to your website.
- Refresh one of the pages from your Drupal website, then right-click the page, and choose the "View Source" option. You should see the Google Analytics code in this source code.
The Drupal Google Analytics installation for this website
This website (devdaily.com) uses a modified version of the Chrysalis Drupal theme, and for my installation, I took this one step further by placing my Google Analytics code into a separate file, and then including it into my theme's page.tpl.php page. So my Drupal Google Analytics installation looks a little more like this:
- I got my Google Analytics HTML code from the Google Analytics website.
- I put my Google Analytics HTML code into a text file in my theme's directory.
- I added a PHP include statement to my theme's
- I then refreshed a page on my website, then viewed the source code of that web page to make sure the Google Analytics code was showing up properly.
Google won't show your website results immediately, so I waited and then checked the Google Analytics website the next day to make sure my data was showing up, and I was in the Google Analytics + Drupal business.
One include statement in my Drupal page.tpl.php file
I just noticed that Google has changed their advice on where to install their Analytics code. When I first wrote this article I placed my Analytics code before the "</body>" tag of my theme, but these days Google recommends inserting the Analytics code before the </head> tag. Regardless of what Google recommends in the future, you'll still want to modify your theme's page.tpl.php page.
For the record, here are the last few lines of my Drupal page.tpl.php file. As you can see, all I did was add a PHP include statement (shown in bold) in between the closing
div tag and the closing
body tag of this
</div><!-- end container div --> <? include "google-analytics.inc" ?> </body>
This PHP include statement includes the Google Analytics code snippet that I got from Google, which I keep in this separate file. I could have embedded the Google Analytics code directly into my Drupal page file, but as you're about to see, this is a much cleaner approach for a more complicated Drupal theme.
Why I used a PHP include
Because this is a simple Drupal theme, I only have to place this PHP include statement in my
page.tpl.php file, and the Google Analytics code is automatically included into every web page that Drupal serves up. However, in a more complicated Drupal theme, you may have to place this PHP include statement in more than one page.
For instance, you may have one page file for you front page, another page file for your login page, etc. But that's where this include approach really shines: all you need to do is add this PHP include statement to the end of each page file in your theme.
The Drupal Google Analytics module
Before I go, I should mention that there is a Drupal Google Analytics module, which you can find at their Drupal project page.
At the moment I don't know why I'd need a Drupal module for Google Analytics support, but you can check out that page for more information on their Drupal Google Analytics module approach. My guess is that their Google Analytics module provides a way to include the Google Analytics code into your Drupal theme without modifying the theme pages, and if so, that may be a good approach for many people.
But for me, because I've already modified my Drupal theme, I prefer my "Drupal Analytics" approach. My guess is that most Drupal users (a) also won't use a stock Drupal theme, and (b) will be comfortable opening a PHP file in a text editor and adding one