This guide provides step-by-step instructions for implementing additional events and parameters in your GA4 property. These enhancements are designed to improve the performance of both your attribution models and machine learning (ML) model.
Parameter setup
To improve the performance of your ML model and attribution, it’s necessary to implement the tracking of several event parameters and user properties in Google Analytics.
General Steps
- Pass the Parameter to dataLayer: For each event or user property, pass the relevant parameter (e.g.,
user_id
,ip_address
,ever_paid
,is_first_purchase
etc.) to yourdataLayer
.
- Send the parameter to Google Analytics via Google Tag Manager (GTM) or gtag.js:
- For GA4 via Tag Manager:
- Tracking user properties:
- If you don’t already use a Google Tag: Event Settings variable, create one.
- Under the User Properties section of your Google Tag, add a new row with the Parameter and the corresponding Value (the Data Layer variable you created).
- Select the created Event Settings Variable in each GA4 configuration and event tag.
- Tracking event parameters:
- Open your GA4 event tag that tracks conversions.
- In the Event Parameter section, add a new row with the Parameter and the corresponding Value (the Data Layer variable you created).
- For GA4 using gtag.js: Add the parameter to the appropriate command in your measurement code:
- Tracking user properties:
- Tracking event parameters:
javascriptgtag('config', '<TAG_ID>', { '<parameter_name>': '<parameter_value>' });
javascriptgtag('event', '<event_name>', { '<parameter_name>': '<parameter_value>' });
Parameters to Implement
- Config parameter - User ID (
user_id
) - Example for
gtag.js
:
Pass a unique user ID after login to track users across sessions on different devices.
javascriptgtag('config', 'STREAM_ID', { 'user_id': 'USER_ID' });
- User property - IP Address (
ip_address
) - Example for
gtag.js
:
Collect the user's IP address. Please note that collecting a user's IP address should only be done after obtaining their explicit consent.
javascriptgtag('config', 'STREAM_ID', { 'user_properties': { 'ip_address': 'IP_ADDRESS' } });
- User property - Ever Paid (
ever_paid
) - Example for
gtag.js
:
Track whether a user has ever converted with a
true
or false
flag.This parameter is necessary freemium subscription based businesses where users can login to the platform on the same website where they can convert, it’s necessary to be able to differentiate all actions of converted customers.
javascriptgtag('config', 'STREAM_ID', { 'user_properties': { 'ever_paid': '<true or false>' } });
- Event parameter - First-Time Purchase (
is_first_purchase
) - Example for
gtag.js
:
Indicate whether the event is a first-time or repeat conversion.
javascriptgtag('event', '<event_name>', { 'is_first_purchase': '<true or false>' });
Creating user scoped custom dimensions
It’s recommended to create user level custom dimensions inside your GA4 admin panel for the user properties
ever_paid
, ip_address
, and isBot
.Follow these steps in your GA4 admin panel:
- Go your GA4 property’s Admin settings.
- In the Data display section, select Custom definitions.
- Click on Create custom dimension.
- Set the Scope to
User
.
- Depending on the property you are setting up, use these settings:
- ip_address:
- Dimension name: IP Address
- Description: User’s IP Address
- User property: ip_address
- ever_paid:
- Dimension name: Ever Paid
- Description: Flag indicating if the user has ever paid
- User property: ever_paid
- isBot:
- Dimension name: Is Bot
- Description: Flag indicating if the user is a bot
- User property: isBot
- Click Save.
Implementing SegmentStream SDK
The SegmentStream SDK script sends additional GA4 events that act as user engagement signals. It also improves the attribution of users who begin browsing your website through in-app browsers that open when ads are clicked within apps, such as Instagram, LinkedIn, etc.
Below is the guide for implementing the script and the GA4 event.
- Inside your Google Tag Manager go to Tags, and click New:
- Name the tag SegmentStream SDK, and choose the Custom HTML tag type:
- Enter the following code and choose All Pages in the Firing Triggers option:
html<script async src="https://cdn.segmentstream.com/js/segmentstream.min.js"></script>
Tracking non_idle events
- Go to Triggers, and click New:
- Name the trigger as it suits you, for example Event - nonIdle, and choose the Custom Event trigger type:
- Enter nonIdle in the Event name field, which is the name of the event pushed into the
dataLayer
by the SDK script. It should look like this:
- In order to collect the non-idle time, we have to create a new variable. Go to Variables, click New:
- Name the variable as nonIdleTimeElapsed, and select the Data Layer Variable type:
- Enter nonIdleTimeElapsed in the Data Layer Variable Name field, your variable should look like this:
- Go to Tags, click New:
- Choose GA4 Event tag type.
- Enter the Measurement ID used by your Google Tag.
- Set Event - nonIdle as the trigger.
- Enter the settings:
- Event Name: non_idle
- Event Parameters:
- value - {{nonIdleTimeElapsed}}
Detecting Bots
- Go to Triggers, and click New:
- Name the trigger as it suits you, for example Event - botDetected, and choose the Custom Event trigger type:
- Enter botDetected in the Event name field, which is the name of the event pushed into the
dataLayer
by the SDK script. It should look like this:
- Go to Variables, click New:
- Name the variable as isBot, and select the Data Layer Variable type:
- Enter isBot in the Data Layer Variable Name field, your variable should look like this:
- Go to Tags, click New:
- Choose GA4 Event tag type.
- Enter the Measurement ID used by your Google Tag.
- Set Event - botDetected as the trigger.
- Enter the settings:
- Event Name: bot_detected
- User Properties:
- isBot - {{isBot}}
- Optional - Create an
isBot
user level dimension by following our guide - Additional GA4 settings.
Configuring SegmentStream SDK Settings
By default, the SegmentStream SDK loads all available modules on your website. If you want to prevent specific modules from loading, you need to configure the settings script before initialising the SDK script.
This configuration is only necessary if you want to disable specific modules. Without it, all modules will be loaded automatically.
Suppose you want to:
- Enable the
collect_activity
module (used for sending thenon_idle
event).
- Disable the
click_id_propagation
module (used for attributing users browsing via in-app browsers).
- Disable the
enable_bot_detection
module (used for identifying bots).
In this case, use the following script:
html<script> window.segmentstream_sdk_settings = { collect_activity: true, // Enable activity event tracking click_id_propagation: false, // Disable attribution improvement enable_bot_detection: false // Disable bot detection }; </script> <script async src="https://cdn.segmentstream.com/js/segmentstream.min.js"> </script>