How to integrate UrbanFox clickstream tracking
Fetch your snippet configuration and deploy clickstream tracking via Google Tag Manager
This guide shows you how to deploy UrbanFox's Clickstream Analytics collector in your Google Tag Manager (GTM) container. You start by fetching the integration snippet and installing the base collector, then configure click capture and user identity handling before testing and publishing.
Before you start
Have these ready:
- A valid access token with
read:snippetpermission - Your
tenant_slug, used in the API host and tenant-scoped request path - Google Tag Manager installed on your website
- A published GTM container for your website
- GTM permissions to create Tags, Triggers, and Variables
- The collector endpoint value provided for your UrbanFox tenant
- Your website can push login and logout events to the GTM data layer
- A modern browser (Chrome, Firefox, Safari)
Fetch your integration snippet
curl 'https://api.YOUR_TENANT_SLUG.urbanfox.io/v2/YOUR_TENANT_SLUG/snippet' \
-H 'Authorization: Bearer YOUR_ACCESS_TOKEN'Response:
{
"data": {
"snippets": [
{
"framework": "HTML",
"snippet": "<script src=\"https://d29oum988gdpan.cloudfront.net/clickstream-web.min.js\"></script>"
}
],
"tenant_name": "YOUR_TENANT_NAME",
"app_id": "YOUR_APP_ID",
"tenant_id": "YOUR_TENANT_ID"
}
}Copy the snippet value. Note the returned app_id and tenant_id; you use both in the GTM setup.
Install the base Clickstream collector tag
Create GTM variables for the IDs returned by the snippet API.
- In the left sidebar, click Variables → User-Defined Variables → New.
- Create a variable named
App ID. Under Variable Configuration, choose Constant and set the value to the exactapp_idfrom the snippet response. - Repeat for a variable named
Tenant ID, set to the exacttenant_idfrom the snippet response.
Now create the collector tag.
-
Log in to Google Tag Manager and select the container for your website.
-
In the left sidebar, click Tags → New.
-
Name the tag
Base Clickstream Collector. -
Under Tag Configuration, choose Custom HTML.
-
Paste the collector script from the
snippetresponse, then add this initialization script below it:<script> window.ClickstreamAnalytics.init({ appId: "{{App ID}}", endpoint: "YOUR_COLLECTOR_ENDPOINT", globalAttributes: { tenant_id: "{{Tenant ID}}", }, }); </script> -
Under Triggering, click the plus icon (+) and select the Initialization - All Pages trigger so the collector runs as early as possible. If it is not listed, create it: choose trigger type Initialization, set it to fire on All Pages, and save. Do not pick the built-in All Pages Page View trigger.
-
Replace
YOUR_COLLECTOR_ENDPOINTwith the collector endpoint value provided for your UrbanFox tenant. -
Click Save.
-
Do not change any other lines in the initialization script.
Create a click event tag
This tag records every click, including clicks on blank areas of the page, with click coordinates. Use a Custom HTML tag with a document-level listener.
Create the tag:
-
Click Tags → New.
-
Name it
Click Event. -
Under Tag Configuration, choose Custom HTML.
-
Paste this snippet:
<script> document.addEventListener("click", function (event) { if (typeof ClickstreamAnalytics === "undefined") return; ClickstreamAnalytics.record({ name: "blank_click", attributes: { xCoordinate: event.clientX, yCoordinate: event.clientY, }, }); }); </script> -
Under Triggering, click the plus icon (+) and select Initialization - All Pages (create it if it does not exist) so GTM registers the listener once, as early as possible. Do not use a Click trigger here.
-
Click Save.
Set the user ID after login
Your website code must push a userLogin event to the GTM data layer after a successful login.
Push the login event into the data layer
Include this in your site's login success handler, immediately after the user logs in:
<script>
window.dataLayer = window.dataLayer || [];
// Replace 'USER_ID_HERE' with your real user-ID variable
window.dataLayer.push({
event: "userLogin",
userId: "USER_ID_HERE",
});
</script>- Add this code in your website, not in GTM.
- Place it right after your login logic (for example, after a successful API response or redirect).
- Do not modify the
event: 'userLogin'name or theuserIdkey; GTM relies on these exact identifiers.
Create the base triggers in GTM
Create two triggers:
| Name | Type | Configuration |
|---|---|---|
Window Loaded | Page View → Window Loaded | n/a |
User Login (Data Layer) | Custom Event | Event Name: userLogin |
Combine them with a trigger group
- Go to Triggers → New.
- Name it
Login Trigger Group. - Under Trigger Configuration, choose Trigger Group.
- Add both included triggers:
Window LoadedandUser Login (Data Layer). - Click Save.
Create the "Set User ID" tag
-
Go to Tags → New.
-
Name it
login_success. -
Under Tag Configuration, choose Custom HTML.
-
Paste this snippet:
<script> var userId; for (var i = 0; i < window.dataLayer.length; i++) { if (window.dataLayer[i].userId !== undefined) { userId = window.dataLayer[i].userId; break; } } if (userId) { if ( typeof ClickstreamAnalytics !== "undefined" && typeof ClickstreamAnalytics.setUserId === "function" ) { window.ClickstreamAnalytics.setUserId(userId); } else { console.log("Clickstream Analytics is not ready"); } } </script> -
Under Triggering, select
Login Trigger Group. -
Click Save.
All subsequent clickstream events are now tied to the correct user ID, without GTM needing to know how your app fetched that ID.
Clear the user ID after logout
Push a logout event from your site's logout handler:
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: "userLogout",
});
</script>Create a logout trigger in GTM:
- Go to Triggers → New.
- Name it
User Logout (Data Layer). - Under Trigger Configuration, choose Custom Event.
- Set Event Name to
userLogout. - Click Save.
Create the logout tag:
-
Go to Tags → New.
-
Name it
logout_success. -
Under Tag Configuration, choose Custom HTML.
-
Paste this snippet:
<script> if ( typeof ClickstreamAnalytics !== "undefined" && typeof ClickstreamAnalytics.setUserId === "function" ) { window.ClickstreamAnalytics.setUserId(null); } </script> -
Under Triggering, select
User Logout (Data Layer). -
Click Save.
Test and publish
- Click Preview in the top-right of GTM to open the Debug Console.
- Enter your site URL and start the preview.
- Navigate between pages and verify the base collector script loads. In your browser's Network tab, look for the
clickstream-web.min.jsrequest from the snippet URL. - Click anywhere and verify the
blank_clickevent fires. In your browser's Network tab, look for a request to the tenant-provided collector endpoint URL you configured in the base tag. In Debug Console → Tags, check theClick Eventtag. - Log in and verify the
login_successtag fires. - Log out and verify the
logout_successtag fires. - If everything looks good, return to GTM and click Submit → Publish.
Result
Your GTM container now loads the Clickstream collector, records click coordinates, sets the user ID after login, and clears it after logout.
See also
- How to authenticate with the UrbanFox API
- API Reference for the snippet endpoint schema