Google Analytics

You can integrate with Google Analytics to create custom segments based on bot and human traffic, then share data enrichment for further analysis. You can then use this to filter bot segments out of your Google Analytics dashboards, allowing your organization to make better decisions without data contamination.

You can learn how to integrate with Google Analytics with this article.

Prerequisites

  • Google Tag Manager (gtag.js) deployed on your application
  • Appropriate access in Google Analytics to update your website client-side code
  • Score reporting enabled and set to binary with a threshold of 100

Google Analytics configuration

To send data to GA4, you need to create custom dimensions, which let you collect and analyze custom data within Google Analytics.

📘

Note

You must create a custom dimension for each data type you want to share with GA4. For example, if you want to send score reporting and two data classification enrichment types, then you need to make three custom dimensions.

  1. Follow Google's steps to Create user-scoped custom dimensions.
  2. While completing the fields to create the custom dimension, use the following:
    • Dimension name: Enter a name you want to use for this dimension.
    • Scope: Select User.
    • Description: Provide a description if desired.
    • User property: Enter a user property. You will use this value later in Client-side JavaScript integration.
  3. Repeat the steps for any additional custom dimensions you need.
  4. Follow Google's steps to Create a custom segment using the custom dimensions you created above. We recommend creating a user segments type of segment.

Client-side JavaScript integration

HUMAN will send an asynchronous event to the client-side environment, containing the result of the bot-or-not decision, as well as any other configured enrichment data. To share this data with Google Analytics, you need to add a new function to the client-side code that receives each of these events, then passes it along to GA4 as an event with custom dimensions.

Below is an example for an implementation for:

  • A custom dimension named bot_flag, and
  • A data enrichment data point named f_id
window.<APPID>_asyncInit = function(px) { // Replace with your application ID
  // Event listener for 'score' events
  px.Events.on('score', function(score) {
    try {
      // Send a custom event to GA4 when a 'score' is received
      // 'bot_defender_score' is a custom event name you define
      // 'score_value' is a custom parameter for this event
      gtag('event', 'bot_defender_score', {
        'score_value': score,
        'event_category': 'Bot_Defender', // Optional: You can categorize events
        'event_label': 'Score_Received' // Optional: Label for more detail
      });

      // Also set a user property based on the score
      gtag('set', 'user_properties', {
        'bot_flag': score // 'bot_flag' is the name of your user-scoped custom dimension in GA4
      });

    } catch (err) {
      // Remove if you do not want to log errors in production
      console.log("error: " + err);
    }
  });

  // Event listener for 'enrich' events
  px.Events.on('enrich', function (value) {
    try {
      const base64Data = value.split(":")[1]; // split value to get the base64 encoded data
      const dataStr = atob(base64Data); // base64 decode the enrichment data
      const data = JSON.parse(dataStr); // get the data as JSON

      // Send a custom event to GA4 for enrichment data
      // 'bot_defender_enrichment' is a custom event name
      gtag('event', 'bot_defender_enrichment', {
        'f_id': data['f_id'],
        'event_category': 'Bot_Defender',
        'event_label': 'Enrichment_Data'
      });

    } catch (err) {
      // Remove if you do not want to log errors in production
      console.log("error: " + err);
    }
  });
};