How to Set Up Google Analytics For Drupal?

12 minutes read

To set up Google Analytics for Drupal, follow these steps:

  1. Sign in to your Google Analytics account or create a new one if you don't have an existing account.
  2. Once signed in, click on the "Admin" tab located at the bottom left corner of the navigation menu.
  3. Under the "Property" column, click on the dropdown menu and select the website for which you want to set up Google Analytics.
  4. Click on the "Tracking Info" tab under the "Property" column.
  5. From the dropdown menu, select "Tracking Code."
  6. You will see a tracking code snippet displayed on the page. Copy the entire code.
  7. In your Drupal website, log in to the administrative dashboard.
  8. Go to the "Extend" section and search for the "Google Analytics" module.
  9. Install and enable the "Google Analytics" module.
  10. Once enabled, go to the "Configuration" section and click on "Google Analytics" in the dropdown menu.
  11. In the "General Settings" tab, paste the tracking code you copied earlier into the "Tracking Code" field.
  12. Configure any additional settings according to your requirements.
  13. Click on the "Save configuration" button to save your settings.
  14. Google Analytics is now set up for your Drupal website. It will start tracking visitor data and providing you with valuable insights into your website's performance.

Best Google Analytics Books In 2024

1
Google Analytics Demystified (4th Edition)

Rating is 5 out of 5

Google Analytics Demystified (4th Edition)

2
Learning Google Analytics: Creating Business Impact and Driving Insights

Rating is 4.9 out of 5

Learning Google Analytics: Creating Business Impact and Driving Insights

3
Google Analytics: Understanding Visitor Behavior

Rating is 4.8 out of 5

Google Analytics: Understanding Visitor Behavior

4
Google Analytics Breakthrough: From Zero to Business Impact

Rating is 4.7 out of 5

Google Analytics Breakthrough: From Zero to Business Impact

5
Google Analytics Alternatives: A Guide to Navigating the World of Options Beyond Google

Rating is 4.6 out of 5

Google Analytics Alternatives: A Guide to Navigating the World of Options Beyond Google

6
Learning Google AdWords and Google Analytics

Rating is 4.5 out of 5

Learning Google AdWords and Google Analytics

7
Data Engineering with Google Cloud Platform: A practical guide to operationalizing scalable data analytics systems on GCP

Rating is 4.4 out of 5

Data Engineering with Google Cloud Platform: A practical guide to operationalizing scalable data analytics systems on GCP

8
Practical Google Analytics and Google Tag Manager for Developers

Rating is 4.3 out of 5

Practical Google Analytics and Google Tag Manager for Developers

9
Advanced Web Metrics with Google Analytics

Rating is 4.2 out of 5

Advanced Web Metrics with Google Analytics


How to exclude specific user IP addresses from being tracked by Google Analytics in Drupal?

To exclude specific user IP addresses from being tracked by Google Analytics in Drupal, you can use the Google Analytics module and create a custom code snippet. Here are the steps:

  1. Install and enable the Google Analytics module in Drupal.
  2. Go to Configuration > System > Google Analytics (admin/config/system/google-analytics) in your Drupal admin panel.
  3. Paste the following code into the "Code Snippet" field:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/**
 * Implements hook_preprocess_HOOK() for html.html.twig.
 */
function YOURTHEME_preprocess_html(&$variables) {
  // Exclude specific IP addresses from Google Analytics tracking.
  $excluded_ips = array(
    '127.0.0.1',
    '10.0.0.1',
    // Add more IP addresses to exclude as needed.
  );

  // Retrieve the current user's IP address.
  $current_ip = \Drupal::requestStack()->getCurrentRequest()->getClientIp();

  // Check if the current user's IP address is in the excluded IPs array.
  if (in_array($current_ip, $excluded_ips)) {
    // Inject the Google Analytics exclusion snippet into the head of the page.
    $variables['page']['#attached']['html_head'][] = [
      [
        '#type' => 'html_tag',
        '#tag' => 'script',
        '#attributes' => [
          'async' => 'async',
          'src' => 'https://www.googletagmanager.com/gtag/js?id=YOUR_GOOGLE_ANALYTICS_TRACKING_ID',
        ],
      ],
      'google_analytics',
    ];
  }
}


  1. Replace YOURTHEME with the name of your Drupal theme.
  2. Replace YOUR_GOOGLE_ANALYTICS_TRACKING_ID with your actual Google Analytics tracking ID.
  3. Modify the $excluded_ips array to include the specific IP addresses you want to exclude from tracking. Add more IP addresses as needed.
  4. Save the configuration.


Now, any user whose IP address matches the ones listed in the $excluded_ips array will not have their activity tracked by Google Analytics.


How to track internal site searches using Google Analytics in Drupal?

To track internal site searches using Google Analytics in Drupal, you can follow these steps:

  1. Install and enable the Google Analytics module in your Drupal site.
  2. Go to the configuration page for the Google Analytics module (Configuration > System > Google Analytics).
  3. Enter your Google Analytics account number in the "Web Property ID" field.
  4. Scroll down to the "Custom Dimensions" section and click on "Add Custom Dimension" button.
  5. In the "Name" field, enter a name for your custom dimension (e.g., "Internal Search").
  6. In the "Scope" field, select "Hit" to track the dimension at the hit level.
  7. Save the changes.
  8. In your Drupal site administration, go to Configuration > Search and Metadata > Search Settings.
  9. Under "Search type," select "Google Analytics" from the drop-down menu.
  10. Scroll down to the "Google Analytics" section and check the "Track internal search" checkbox.
  11. In the "Track types" field, enter the types of search results pages that you want to track (e.g., node, view, etc.).
  12. Save the configuration.
  13. Clear the Drupal cache (Configuration > Development > Performance > Clear all caches).
  14. Wait for the changes to take effect (usually within 24 hours) and then log in to your Google Analytics account.
  15. Go to the "Behavior > Site Search > Search Terms" report to view the internal search terms tracked by Google Analytics.


By following these steps, you will be able to track internal site searches using Google Analytics in Drupal.


How to track download links and file extensions in Google Analytics for Drupal?

To track download links and file extensions in Google Analytics for Drupal, you can follow these steps:

  1. Install and activate the Google Analytics module in your Drupal site.
  2. Go to your Google Analytics account and enable ‘Events’ tracking. This will allow you to track specific events, such as file downloads.
  3. In your Drupal site, go to Configuration > System > Google Analytics.
  4. Enter your Google Analytics Tracking ID in the appropriate field.
  5. Scroll down to the 'Event tracking' section and check the box to enable it.
  6. In the 'Files' field, enter the file extensions that you want to track. For example, if you want to track PDF files, enter '.pdf' without quotes.
  7. Save the configuration.
  8. Next, you need to modify the link tags in Drupal to track the downloads properly. You can do this by adding the necessary JavaScript code to the link tags.
  9. Locate the link or button tags that you want to track (e.g., download links) in Drupal's template files or content types.
  10. Add the following code to the link tags:
1
onClick="_gaq.push(['_trackEvent', 'Download', 'Click', 'File Name'])"


Replace 'File Name' with a descriptive name for the file you are tracking. 11. Save the changes to the template files or content types and clear the Drupal cache. 12. Test the tracking by visiting your Drupal site and clicking on the download links. You should see the events being recorded in your Google Analytics account under Behavior > Events.


Note: The process may vary depending on the version of Drupal and the theme you are using. It is recommended to make a backup of your theme files before making any modifications.


How to enable/disable the Google Analytics module in Drupal?

To enable or disable the Google Analytics module in Drupal, follow these steps:

  1. Log in to your Drupal website as an administrator.
  2. Navigate to the "Extend" page by clicking on "Extend" in the top toolbar or by going to "/admin/modules" in your Drupal website's URL.
  3. On the "Extend" page, you will see a list of all the installed modules. Look for the "Google Analytics" module in the list. It may be located under the "Contributed" section.
  4. To enable the module, check the box next to "Google Analytics" in the list.
  5. Scroll down to the bottom of the page and click on the "Install" button. This will enable the module on your Drupal site.
  6. To configure the Google Analytics module, click on the "Configure" link next to the "Google Analytics" module in the Modules list.
  7. On the module's configuration page, you will be prompted to enter your Google Analytics tracking ID. This ID can be obtained from your Google Analytics account.
  8. Enter your Google Analytics tracking ID in the provided field and click on the "Save configuration" button to save the changes.
  9. The Google Analytics module is now enabled on your Drupal website, and it will start tracking visitor data.


To disable the Google Analytics module:

  1. Follow steps 1 to 3 above to navigate to the "Extend" page in your Drupal admin dashboard.
  2. In the "Extend" page, uncheck the box next to "Google Analytics" in the list of modules.
  3. Scroll down to the bottom of the page and click on the "Uninstall" button. This will disable the Google Analytics module on your Drupal site.
  4. The Google Analytics module is now disabled, and it will no longer track visitor data on your Drupal website.
Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Google Analytics 4 (GA4) is the latest version of Google&#39;s web analytics platform. It offers enhanced features and capabilities compared to its predecessor, Universal Analytics. Here&#39;s a general overview of how to use Google Analytics 4:Set up a Google...
To set up Google Analytics on a website, you need to follow these steps:Sign up for a Google Analytics account: Go to the Google Analytics website (analytics.google.com) and click on &#34;Start for free.&#34; Sign in with your Google account or create a new on...
To access the Google Merchandise Store Analytics, you need to follow these steps:Open your web browser and visit the Google Analytics website (https://analytics.google.com/).Sign in to your Google account. Make sure you use the same account associated with the...