Get Started Log in
Docs / GA4 through Google Tag Manager

GA4 through Google Tag Manager

Send Sourcream's first and last touch to GA4 as user scoped custom dimensions, so GA4 reports leads by the same source, medium and campaign your CRM holds.

The Sourcream script itself should be in your site's <head> (see Custom HTML sites). It can be loaded through GTM with a Custom HTML tag on the Initialization, All Pages trigger, but then anything that blocks GTM also blocks Sourcream, and it starts later on every page. Install it directly where you can.

Once the values are final on each page (after our server has confirmed the visitor), the script pushes:

window.dataLayer.push({
  event: 'sourcream_ready',
  sourcream: {
    first_source: 'google', first_medium: 'cpc', first_campaign: 'spring_sale',
    last_source: 'linkedin', last_medium: 'social', last_campaign: '(none)',
    sc_visitor_id: 'v_k3j9x0a1b2c3d4e5f6g7'
    // plus the extended values when the site uses the extended field set
  }
});

1. Variables

In GTM, Variables, User-Defined Variables, New, Data Layer Variable, version 2. Create one per value:

Variable name Data Layer Variable Name
DLV - First Source sourcream.first_source
DLV - First Medium sourcream.first_medium
DLV - First Campaign sourcream.first_campaign
DLV - Last Source sourcream.last_source
DLV - Last Medium sourcream.last_medium
DLV - Last Campaign sourcream.last_campaign

2. Trigger

Triggers, New, Custom Event, event name sourcream_ready, fires on all custom events. Name it CE - sourcream_ready.

3. Tag

Tags, New, Google Analytics: GA4 Event:

  • Measurement ID: your GA4 measurement ID (or your Google tag).
  • Event name: sourcream_attribution.
  • User Properties: first_source = {{DLV - First Source}}, first_medium = {{DLV - First Medium}}, first_campaign = {{DLV - First Campaign}}, last_source = {{DLV - Last Source}}, last_medium = {{DLV - Last Medium}}, last_campaign = {{DLV - Last Campaign}}.
  • Trigger: CE - sourcream_ready.

The event fires once per page view. If you would rather not add an event per page, set the same user properties in your Google tag's configuration with the cookie variables below instead (they hold values from the second page view onwards).

4. GA4 custom dimensions

In GA4, Admin, Data display, Custom definitions, Create custom dimension, once per value:

Dimension name Scope User property
First source User first_source
First medium User first_medium
First campaign User first_campaign
Last source User last_source
Last medium User last_medium
Last campaign User last_campaign

Do not register the visitor ID as a dimension: every visitor has a different one, so it adds nothing to reports and uses up your dimension quota. New dimensions fill from the day you create them, not retrospectively.

5. Test

Open GTM Preview, load your site with ?utm_source=test&utm_medium=test&utm_campaign=gtm, and check that sourcream_ready appears in the event list with the values, and that the sourcream_attribution tag fired. In GA4 DebugView, the event carries the user properties.

The first and last touch are also stored in the first-party cookies _sc_ft and _sc_lt as URL encoded JSON with short keys: s source, m medium, c campaign, t term, n content, i campaign id, k click ids (an object, for example {"gclid":"..."}), l landing page, r referrer, ts time in epoch milliseconds.

One Custom JavaScript variable per value. Last source:

function () {
  var match = document.cookie.match(/(?:^|;\s*)_sc_lt=([^;]*)/);
  if (!match) return undefined;
  try {
    return JSON.parse(decodeURIComponent(match[1])).s;
  } catch (e) {
    return undefined;
  }
}

Use _sc_ft for first touch, and .m or .c for medium and campaign. Name them JS - Last Source, JS - First Medium and so on. The site's Install tab in Sourcream shows all six ready to paste.

Cookie variables are empty on the very first page view until the script has run, and always empty when the visitor has not consented and the script is running in memory only mode (Consent). The sourcream_ready event has no first page gap, but it waits for consent too: in memory only mode it is not pushed at all, and once the visitor consents it is pushed on that same page. With consent mode set to Required or Google Consent Mode, visitors who have not consented never trigger CE - sourcream_ready.

Why GA4 and Sourcream can still differ

  • GA4 attributes sessions and users; Sourcream attributes leads. A user scoped dimension holds the latest value GA4 received for that user.
  • GA4 may not see visitors whose browser blocks Google Analytics, while Sourcream may still capture their leads, particularly in first-party mode.
  • GA4 applies its own consent and modelling.

For offline conversion imports into Google Ads, set the Field set of your CRM or webhook integration to Extended (Integrations, the integration's settings; Business plan and above). Sourcream then sends gclid, gbraid and wbraid with each lead it delivers; HubSpot, Salesforce, Pipedrive and Zapier, Monday.com and webhooks describe the fields each destination receives. The site's field set does not change what Sourcream delivers: it only decides which hidden fields the script fills in your forms and which values the sourcream_ready event carries. Switch the site to Extended as well only when your form tool carries the click ids into your CRM through hidden fields.

Questions: [email protected]

Stuck on an install? Email [email protected] with your site address and what you are seeing, and we will help you get it working.