Using Google Analytics with Drupal
Learn how to use Google Analytics with your Drupal site to get details about your website traffic. Follow our step-by-step guide to get started.
This article discusses several ways you can use Google Analytics with a Drupal site.
Using Google Analytics with Drupal
Google Analytics provides detailed statistics about visitor traffic to your site. To use Google Analytics with Drupal (or with any other type of site), you must first sign up for a Google Analytics account at http://www.google.com/analytics and obtain a unique Tracking ID.
After you have set up a Google Analytics account and have the Tracking ID for your site, you can configure it for Drupal. There are two ways to do this:
-
Use a Drupal module.
-
Edit the Drupal theme.
Method #1: Use a module
There are several modules available that enable you to configure Google Analytics for a Drupal site, and they all function in essentially the same way—all you have to do is specify your Tracking ID, and the module takes care of the rest.
A popular Drupal module for Google Analytics is the aptly named Google Analytics module. To install and configure this module, follow these steps:
-
Use your web browser to visit https://www.drupal.org/project/google_analytics.
-
Download the.zip file for your Drupal version to your local computer.
-
Log in to your Drupal site as the administrator.
-
On the top menu bar, click Modules, and then click Install new module.
-
Click Browse, select the.zip file on your local computer, and then click Install. Drupal installs the module.
-
On the top menu bar, click Modules.
-
Under STATISTICS, locate Google Analytics, select the ENABLED check box, and then click Save configuration.
-
On the top menu bar, click Configuration.
-
Under SYSTEM, click Google Analytics. The Google Analytics configuration page appears.
-
Under GENERAL SETTINGS, in the Web Property ID text box, type your Google Analytics tracking ID.
-
In the Tracking scope section, you can configure additional settings to customize your site's tracking behavior.
-
Click Save configuration. Google Analytics is now enabled for Drupal.
Note
It can take up to 24 hours for Google Analytics to change its status from
Tracking Not Installed to
Receiving Data .
Method #2: Edit the theme
Modules are convenient, but they also carry processing overhead with them that may affect site performance. If you prefer not to use a module, you can edit the Drupal theme directly to enable Google Analytics. By embedding a Google Analytics JavaScript code snippet in the theme, you can make sure that statistics gathering is enabled for every page.
To do this, follow these steps:
-
Log in to your Google Analytics account, click Admin, click Tracking Info, and then click Tracking Code. Copy the entire text in the tracking code box, including the starting tag and the ending tag.
-
Using SSH or the cPanel File Manager, navigate to the ~/public_html/themes/theme_name/templates directory, where theme_name represents the theme currently enabled for your site.
Note
If you installed Drupal in a subdirectory beneath public_html, navigate to that directory instead, and then navigate to the themes/theme_name/templates directory.
-
Use your preferred text editor to open the page.tpl.php file.
-
At the top of the page.tpl.php file, paste the Tracking ID code snippet from Google Analytics that you obtained in step 1.
Important
Make sure you include the starting
tag and the ending
tag. -
Save your changes to the page.tpl.php file. Google Analytics is now enabled for Drupal.
Note
It can take up to 24 hours for Google Analytics to change its status from
Tracking Not Installed to
Receiving Data .
More Information
For more information about Google Analytics, please visit http://www.google.com/analytics.
Updated 3 days ago