Google Analytics v4 Measurement Protocol - how to send tech and acquisition data

Viewed 952

Background:

I am tracking acquisition data to a mobile application. Certain links redirect to App Store or Google Play store through my website, where Google Analytics collects data on those redirects.

The mobile application also utilizes a new iOS feature called "App Clips", which basically an app fragment that just opens without user needing to go through store download. When the app clip launches, the "middleman" redirecting website is never opened.

In order to have coherent data on Android and iOS acquisition, I wanted to collect the page_view event via Measurement Protocol, triggered directly from the "App Clip".

Problem:

The GA v4 Measurement Protocol API is in alpha. The page_view event is actually missing from the docs, but it can be sent nonetheless.

I want to understand how to populate the event parameters in the request in order to send data required in "Tech" and "Acquisition" categories. This means at least user-agent data, exact device model, campaign, medium and source.

{
    "client_id": "<what_goes_here?>",
    "timestamp_micros": "1627192577008000",
    "non_personalized_ads": false,
    "events": [
        {
            "name": "page_view",
            "params": {
                "page_title": "App Clip",
                "language": "en-us",
                "page_location": "https://example.com/x?utm_campaign=my_campaign",
                "screen_resolution": "1170x2532",
                //"page_referrer": ""
                "page_path": "/x?utm_campaign=my_campaign"
            }
        }
    ]
}

I do not want to use Firebase SDK inside the App Clip for two reasons:

  1. I want the App Clip to be lightweight and Apple discourages usage of 3rd party libraries
  2. I could not be assured that sending screen_view will render the same analytical depth (the same parameters) as page_view.
1 Answers

The client ID refers to the google client ID generated by the gtag script on the frontend side.

Assume for example you have a tag manager or just added an analytics tag to your website. This generates a cookie on the browser (by default is is named _ga_<ID> - where the ID can be retrieved when setting up your analytics property. Note: you can easily customize the name of the cookie). This cookie includes the client ID. When you communicate to your backend API to trigger events, you need to forward this value, either directly by including cookies in your API calls, or by reading the cookie via JS (which is not HttpOnly) and sending the value along with other params.

The cookie value looks like GS1.2.163179xxxx.18.0.163179xxxx.0

Then extract the client ID and add it to your payload.

In our ruby/rails code we extract the client ID with

google_analytics_client_id = context[:controller].request.cookies[Settings.google_analytics_prefix]&.split('.')&.last(2)&.join('.')

then you can send it to the measurement protocol. Our code looks like

def event_payload(user:, event_name:, google_analytics_client_id:, event_params: {}, event_time:)
  {
    user_id: user.id.to_s,
    timestamp_micros: (event_time.to_f * 1_000_000).to_i,
    non_personalized_ads: false,
    user_properties: {
      your_property: { value: user.some_property },
    },
    client_id: google_analytics_client_id,
    events: [
      { name: event_name }.tap do |h|
        h[:params] = event_params if event_params.any?
      end,
    ],
  }
end

I'm not 100% sure this is working since we seem to have problems on our side (it does create events for users, but they do not get UTM attribution correctly for instance), but maybe it has to do with the limitations

Related