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
dataLayerby 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
dataLayerby 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_botuser 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. When tracking purchases, replace
ORDER_IDandORDER_VALUEwith the variables used in your GA4 conversion tag foridandvalue. - For conversions with an order ID (e.g., purchases):
- For conversions without an order ID (e.g., leads):
- For conversions with a value but no order ID:
html<script> segmentstream.conversion({ id: 'ORDER_ID', type: 'purchase', value: ORDER_VALUE }); </script>
html<script> segmentstream.conversion({ type: 'lead' }); </script>
html<script> segmentstream.conversion({ type: 'signup', value: ORDER_VALUE }); </script>
- Set the trigger for this tag to match the trigger used by your GA4 conversion 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({ id: 'ORDER_ID', // Optional: unique identifier for the conversion type: 'purchase', // Required: event type (e.g., 'purchase', 'lead', 'signup') value: ORDER_VALUE // Optional: numeric value of the conversion });
Examples:
- E-commerce purchase with order ID and value:
javascriptsegmentstream.conversion({ id: 'ORD-12345', type: 'purchase', value: 199.99 });
- Lead conversion without ID or value:
javascriptsegmentstream.conversion({ type: 'lead' });
- Subscription with ID but no value:
javascriptsegmentstream.conversion({ id: 'SUB-67890', type: 'subscription' });
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
_gacookie 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
_gacookie
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.
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_activitymodule (used for sending thenon_idleevent).
- Toggle the
click_id_propagationmodule (used for attributing users browsing via in-app browsers).
- Toggle the
enable_bot_detectionmodule (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>