How to use Google Analytics with Drupal

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:

  1. Use your web browser to visit https://www.drupal.org/project/google_analytics.
  2. Download the .zip file for your Drupal version to your local computer.
  3. Log in to your Drupal site as the administrator.
  4. On the top menu bar, click Modules, and then click Install new module.
  5. Click Browse, select the .zip file on your local computer, and then click Install. Drupal installs the module.
  6. On the top menu bar, click Modules.
  7. Under STATISTICS, locate Google Analytics, select the ENABLED check box, and then click Save configuration.
  8. On the top menu bar, click Configuration.
  9. Under SYSTEM, click Google Analytics. The Google Analytics configuration page appears.
  10. Under GENERAL SETTINGS, in the Web Property ID text box, type your Google Analytics tracking ID.
  11. In the Tracking scope section, you can configure additional settings to customize your site's tracking behavior.
  12. Click Save configuration. Google Analytics is now enabled for Drupal.
    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:

  1. 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 <script> tag and the ending </script> tag.
  2. 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.
    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.
  3. Use your preferred text editor to open the page.tpl.php file.
  4. At the top of the page.tpl.php file, paste the Tracking ID code snippet from Google Analytics that you obtained in step 1.
    Make sure you include the starting <script> tag and the ending </script> tag.
  5. Save your changes to the page.tpl.php file. Google Analytics is now enabled for Drupal.
    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.

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.