Skip to main content

Source / medium rules

Why do we need source / medium rules#

GA4 export data already has extracted source, medium and campaign keys inside event_params column. But in some cases, you need to apply custom rules to define source / medium. For example, if for some reason traffic doesn't have correct UTM parameters but has auto generated platform click id, like fbclid. In this case, you need custom logic to define source / medium.

Also, sometimes default attribution rules are not clear. For example, you could notice sessions with gclid but not with google / cpc. And now Google Ads uses not only gclid but also gbraid, wbraid and other query parameters that indicate that the click came from Google Ads.

So you need to define your own source / medium rules.

note

There is one important topic about source / medium rules. GA4 doesn't break sessions if source / medium are changed during the session. The package returns source / medium based on the first founded values. It's a potential improvement for the package to also provide all found source and medium to let end-users create custom session definitions or attribution models.

How package define source / medium#

Packege processes raw GA4 data in a few steps. One of the steps is sessions_with_source_medium_and_lp. In this step we apply source / medium rules to create last click attribution.

By default, the package has a few source / medium rules:

  • if gclid key exists in event_params and is not null then the source / medium set to google / cpc
  • if source, medium, campaign keys exist in event_paramsand are not null then the source / medium set to values from these params
  • if session_referrer exists and is not null the source set to the value of session_referrer and medium to referral
note

Session referre is the first not null value from event_params with key page_referrer. But also package checks if the value is not ignore_referrer is not true. GA4 has automatic self-referral detection

The rules apply in the order they are defined and the first rule which is true is applied.

Read more about processing steps

Custom source / medium rules#

You could add custom source / medium rules using sourceMediumRules property.

Here is an example of how to add a new source / medium rule if gclid is found in the page_location (not in event_params).

const sessions = new ga4.Sessions(sessionConfig);// Add column with query parameter gclidsessions.addQueryParameters([{ name: "gclid", columnName: "gclid_url" }]);
// Add sourceMediumRule for gclidsessions.sourceMediumRules = [  {    columns: ["gclid_url"],    conditionType: "NOT_NULL",    conditionValue: "",    value: {      source: "'google'",      medium: "'cpc'",      campaign: "campaign",    },  },  ...sessions.sourceMediumRules,];sessions.publish();

Here we add new rule in sourceMediumRules. The order is important, if we add a new rule at the end it wouldn't work as default source medium rule would be applied first.

Read more about sourceMediumRules