On this page
- The Google Analytics Module is Deprecated
- Installation
- Adding a module to your site
- Composer
- Drupal.org (not recommended)
- Installing / Enabling the module
- Using the Drupal UI (/admin/modules)
- Using Drush (Command Line)
- Features
- Page and Domain tracking
- Role Tracking
- Link tracking
- Search and Adsense
- Privacy
- Custom variables
- Advanced Settings
- Custom code snippets
(Deprecated) Using Google Analytics 4.x
The Google Analytics Module is Deprecated
This module is becoming the Google Tag module. The 2.0.x branch supports Google Analytics via Google Tag. Users of Google Analytics should be using the 2.x version of this module.
Installation
Adding a module to your site
Composer
To install this module, first add it to your site by requiring it in your composer.json file:
$ composer require 'drupal/google_analytics:^4.0'
Drupal.org (not recommended)
You can download the module from the drupal.org project page, however this is not recommended because it doesn’t bring in any current or future dependencies.
Download and uncompress the module to (webroot)/modules/contrib/google_analytics
Installing / Enabling the module
Using the Drupal UI (/admin/modules)
-
Navigate to the Extend page (admin/modules) via the Manage administrative menu
-
Locate the google analytics module and check the box next to it
-
Click Install to enable (turn on) the new module.
Using Drush (Command Line)
-
Open up a terminal and change directory into your site’s webroot.
-
Execute the following command (note the underscore in the name):
$ drush en google_analytics -y
Features
In addition to the basic page tracking, the google analytics module provides site admins with built in functionality to enhance your GA experience without the need for code.
This module also provides an php event model for developers to implement their own features that aren’t covered in the module.
Page and Domain tracking
Domain tracking allows the javascript to adjust which properties are being tracked based on the domains this site represents. The default is a single site, based on the current domain.
Pages to track is a drupal specific setting, which defines where the javascript will execute. By default, administrative pages are excluded.![]()
Alternatively, you may invert the functionality if you want the GA code to only execute on specific pages. The paths below the options are used for either option. Ensure you verify the paths in the textbox match only pages you wish to be tracked.
Role Tracking
By default, Google Analytics will track all users on a site. Similar to page tracking, you can choose whether to add or omit roles by choosing the appropriate option. If no roles are checked, all users are tracked and the options do not have any effect.
To omit groups of users by Drupal role, choose ‘Add to every role except the selected ones’ and check the role name on the settings page.
To only select a specific group of users, choose ‘Add to the selected roles only’ and check the role name on the settings page.
The roles below the options are used for either option. Ensure you verify the roles after checking either option.
![]()
Link tracking
The module provides functionality to track downloads of files, mailto and outgoing links on your site. Select the relevant options on the main Google Analytics settings page.
A textfield is also provided where you can specify a list of file extensions that should be tracked. Separate each file extension with a '|' character pdf|zip|doc or better leave the defaults.
To see the link tracking reports, go into your Google Analytics dashboard and click on the main Content menu item on the left. That should reveal the Event Tracking option.
Search and Adsense
Allows you to track internal search and track AdSense ads. These features are for existing UA accounts only. For GA4, setup search and adsense within the Google Analytics dashboard.
Privacy
For UA accounts, you can choose to anonymize visitor IP addresses. Due to various local regulations, GA4 includes this feature by default and cannot be changed.
Custom variables
Allows you to send custom variables along with the Google Analytics tracking request.
Metrics and Dimensions are primarily UA constructs. For GA4, only the name and value are recognized. However, as of 4.0.0, you must select ‘metric or dimension’ for the variable to be recognized.
Advanced Settings
Advanced settings for Google Analytics are hidden away in the collapsed fieldset. Expand it and you'll find the following additional features.
Custom code snippets
If you wish to inject additional custom Google Analytics JavaScript code snippets, two textareas are provided to add in code snippets. Just remember to remove the <script> ... </script> tags from the start and end of your snippet! The "before" textarea sets variables prior to the actual trackPageview, then the "after" handles variables, and additional actions (like tracking in multiple accounts).
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion
Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.