The Google Analytics module for Drupal does exactly what you would expect… it lets you add the Analytics code to your site without having to get dirty in any code!
In addition to standard Google Analytics tracking this module lets you track users by profile fields & file downloads which can be really powerful.
The steps to get Google Analytics working are simple:
- Download the module and unzip the file into your /sites/all/modules or /sites/{yoursitename}/modules.
- Log into your drupal site with an account that has access to add modules.
- Go to Administer -> Modules (or just type in the url of admin/build/modules)
- Scroll down to the “Other” section and click on the “Enabled” checkbox next to Google Analyitcs.
Now make sure you have already signed up for a Google Analytics account and have your UA code ready.
To configure the module:
- Go to the administration section -> Site Configuration -> Google Analytics
- Enter in your UA number. If you don’t know where to get your UA user id: Log into Google Analytics, click on Edit to the right of the site profile you setup. Click on Check Status and then click on Legacy Tracking Code or New Tracking Code (both will give you the same UA id). Now just copy the UA-#####-# from the code and paste it into the corresponding textbox in your drupal setup. or
- Uncheck the “Legacy Google Analytics” checkbox since the legacy code is no longer updated.
- Select the roles that you would like to track. (I usually just check all of them) Note: This is different from prior versions. You must explicitly set what roles you want tracked. Note: Your roles may, and most likely will look different than this screenshot.
- If you wish to use User Segmentation, make sure you have the Profile Module enabled (it’s from core Drupal so there’s nothing additional to download) and select the fields you would like reported to Google Analytics. Remember, the more fields you add in the profile settings the more options you’ll have in this select list.
- If you want to track file types, add in the file extensions (without the dot) separated by pipes “|”.
- Click on Save configuration and everything should be setup!
To verify that the module is working:
- Go to any page that isn’t in the administration section since the Google Analytics module does not track /admin or it’s children.
- View the source
- Scroll to the bottom
- Look for some Javascript code that contains your UA id. If it’s there, than it was successful.
A note for themers, make sure your theme prints the $closure variable right before the </body> tag or else your Google Analytics code will never be output (it will also mess up some other modules).