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 that will be provided by your SegmentStream account manager.
html<script>(function(){var s=window.segmentstream=window.segmentstream||{}; s._q=s._q||[];['init','conversion'].forEach(function(m){ s[m]=function(){s._q.push([m,Array.prototype.slice.call(arguments)])}})})();</script> <script async src="https://cdn.segmentstream.com/js/segmentstream.min.js"> </script> <script>segmentstream.init("ID PROVIDED BY MANAGER");</script>
- Expand the Advanced Settings and select Once per page in the Tag firing options.
- Choose All Pages in the Firing Triggers option.
Tracking non_idle events
- Go to Triggers, and click New:
- Name the trigger as it suits you, for example Event - non_idle, and choose the Custom Event trigger type:
- Enter non_idle 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 - non_idle as the trigger.
- Enter the settings:
- Event Name: non_idle
- Event Parameters:
- value - {{nonIdleTimeElapsed}}
The event name must be exactly
non_idle
(case-sensitive), and the event parameter name should be exactly value
. Any other variations will not be recognised by the system.Detecting Bots
- Go to Triggers, and click New:
- Name the trigger as it suits you, for example Event - bot_detected, and choose the Custom Event trigger type:
- Enter bot_detected 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 is_bot, and select the Data Layer Variable type:
- Enter is_bot 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 - bot_detected as the trigger.
- Enter the settings:
- Event Name: bot_detected
- User Properties:
- is_bot - {{is_bot}}
The event name must be exactly
bot_detected
(case-sensitive), and the user property name should be exactly is_bot
. Any other variations will not be recognised by the system.- Optional - Create an
is_bot
user level dimension by following our guide - Additional GA4 settings.
Aggregated Conversions Measurement
The SegmentStream SDK supports conversion tracking in a way that complies with modern privacy regulations. It enables attribution and performance measurement even for users who haven’t provided consent for cookies. This is achieved through an aggregated tracking method that does not rely on cookies or any personally identifiable information (PII).
Implementation through GTM
- Inside your Google Tag Manager go to Tags, and click New:
- Name the tag SegmentStream Conversion Tracking, and choose the Custom HTML tag type.
- In the HTML block, insert the code below. Replace
ORDER_ID
andORDER_VALUE
with the variables used in your GA4 Purchase tag fortransaction_id
andvalue
.
Note: ORDER_ID is used only on the client side to avoid duplicate conversions and is not sent to the server.
html<script>segmentstream.conversion('ORDER_ID', ORDER_VALUE);</script>
If you are tracking an event without an ID or value, such as a lead, use the following code:
html<script>segmentstream.conversion();</script>
- Set the trigger for this tag to match the trigger used by your GA4 Purchase tag.
- In Tag Sequencing, make sure the SegmentStream SDK tag fires before this tag.
- Save and Publish the changes.
Implementation via SDK
You can manually trigger a conversion using the following method:
javascriptsegmentstream.conversion('ORDER_ID', ORDER_VALUE);
- Replace
'ORDER_ID'
with a unique identifier for the conversion event (e.g., an order ID).
- Replace
ORDER_VALUE
with the corresponding numeric value.
Note: ORDER_ID is used only on the client side to avoid duplicate conversions and is not sent to the server.
This function should be added on the conversion confirmation page (e.g., the order success or thank-you page), after the SegmentStream SDK is initialised.
Sending Lead Data to your CRM
When sending lead data from your website to your CRM, you should also send the user identifier provided by the SegmentStream SDK.
Instead of using the
_ga
cookie value, pass the value returned by the following method:javascriptwindow.segmentstream.anonymousId()
- If the user has consented to cookies, this method will return the
_ga
cookie value.
- If the user has not consented, it will return a generated anonymous ID (instead of
null
).
This approach ensures that SegmentStream’s aggregated attribution works for both consented and non-consented users.
How to implement:
- Replace the
_ga
cookie
If your CRM currently receives the
_ga
cookie value, replace it with window.segmentstream.anonymousId()
.- If replacement is not possible
Send the
anonymousId()
value to a separate field in your CRM.- Trigger the Conversion Event on the Same Page
On the same page where the lead is sent to the CRM, you must also call
window.segmentstream.conversion()
.Calling the conversion method on a different page, for example, a redirect page after the lead is sent, will not work for aggregated conversion attribution. The call must happen on the same page where the lead is submitted.
- Usage Example
javascript// code that sends data to the CRM const clientId = window.segmentstream.anonymousId(); sendLeadToCRM({ name: leadName, email: leadEmail, clientId: clientId }); // code that sends data to SegmentStream window.segmentstream.conversion();
This ensures consistent attribution coverage while remaining compliant with privacy regulations.
Special Cases in Conversion Tracking
The
segmentstream.conversion()
method supports flexible parameter usage for different tracking scenarios:- Multiple Conversion Types
- If you want to track several different types of conversions, use the optional third parameter to specify the conversion name:
javascriptsegmentstream.conversion('ORDER_ID', ORDER_VALUE, 'CONVERSION_NAME');
- No Parameters Available
- If your conversion doesn’t have an order ID, value, or name, you can call the method without any arguments:
javascriptsegmentstream.conversion();
- Skipping Parameters
- If a specific parameter is missing but the next one exists (e.g., no order ID but there is a value), pass
null
for the missing parameters:
javascriptsegmentstream.conversion(null, ORDER_VALUE);
This flexibility ensures you can accurately track conversions regardless of the available data structure on your website.
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.
You can control the following modules:
- Toggle the
collect_activity
module (used for sending thenon_idle
event).
- Toggle the
click_id_propagation
module (used for attributing users browsing via in-app browsers).
- Toggle the
enable_bot_detection
module (used for identifying bots).
Example script:
html<script>(function(){var s=window.segmentstream=window.segmentstream||{}; s._q=s._q||[];['init','conversion'].forEach(function(m){ s[m]=function(){s._q.push([m,Array.prototype.slice.call(arguments)])}})})();</script> <script> window.segmentstream_sdk_settings = { collect_activity: true, // activity event tracking click_id_propagation: true, // attribution improvement enable_bot_detection: true // bot detection }; </script> <script async src="https://cdn.segmentstream.com/js/segmentstream.min.js"> </script> <script>segmentstream.init("ID PROVIDED BY MANAGER");</script>