GA4 - Correct way to send UTM campaign data using Measurement protocol?

1.2k Views Asked by At

Im missing something with the UTM information using GA4's Measurement protocol.

With the old Universal Analytics, this data only needed to be set through the url (utm_source=sourceValue&utm_medium=mediumValue&utm_campaign=campaignValue), and will be stored in the user_id session automatically, so when i was using Mesurement protocol calls from my backend i only needed to give the user_id and the UTM parameters will populate automatically.

But with GA4 I don't find any way to do my backend calls with this UTM data also sending client_id and user_id parameters.

I found ways to set UTM parameters using gtag but it doesen't work the same way using Measurement protocol... I also tried doing the call using get parameters https://www.google-analytics.com/mp/collect?cn=campaignValue&cs=campaignSource&cm=campaignMedium&measurement_id=${measurement_id}&api_secret=${api_secret} and post parameters it works fine with the params (cs cn cm), but im unable to find the campaign information in GA4 dashboards.

Anyone struggling with this?

1

There are 1 best solutions below

0
Khoi Ngo On

Using campaign_details event to send campaign params: https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference/events#campaign_details.

Important: session_id must also be added to. It can be acquired from a GA cookie variable: _ga_<GA measurement_id>, the first large number. For example, GS1.1.1704773506.16.1.1704777354.0.0.0 // 1704773506 is what you need.

This API function can extract it:

gtag('get', '<Measurement ID>', 'session_id', (id) => {})

Example request body:

{
   "client_id": "123456.7654321",
   "events": [
     {
        "name": "campaign_details",
        "params": {
          "campaign_id": "google_1234",
          "campaign": "Summer_fun",
          "source": "google",
          "medium": "cpc",
          "term": "summer+travel",
          "content": "logolink",
          "session_id": 1231313, // must be integer value
          "engagement_time_msec": "100"
        }
     },
     {
      "name": "purchase",
      "params": {
        "currency": "USD",
        "transaction_id": "T_12345",
        "value": 12.21,
        "coupon": "SUMMER_FUN",
        "shipping": 3.33,
        "tax": 1.11,
        "session_id": 1231313,
        "items": [
          {
            ...
          }
        ]
      }
   ]
  }

This video has more information about this: https://www.youtube.com/watch?v=r_eoeU2qUn0