{"id":5461,"date":"2022-11-30T11:04:58","date_gmt":"2022-11-30T08:04:58","guid":{"rendered":"https:\/\/trackingchef.com\/?p=5461"},"modified":"2022-11-30T11:04:59","modified_gmt":"2022-11-30T08:04:59","slug":"adding-ga4-to-your-site-using-segment","status":"publish","type":"post","link":"https:\/\/trackingchef.com\/google-analytics\/adding-ga4-to-your-site-using-segment\/","title":{"rendered":"Adding GA4 to your site using Segment"},"content":{"rendered":"\n

Over the past year, I’ve worked with almost a dozen different clients that migrated their Google Analytics implementation from UA to GA4 using Segment<\/a>. While I’m generally fond of Segment and find implementing simple tags with it rather stable, its GA4 Destination is a mess. In this post, I’ll try to explain why it’s so bad and how you can go about implementing it properly.<\/p>\n\n\n\n

Segment’s GA4 Destination<\/h2>\n\n\n
\n
\"image-5440967\"
Image source: Segment<\/a><\/figcaption><\/figure><\/div>\n\n\n

Segment has plenty of Destinations prebuilt and ready for immediate & easy deployment, one of which is of course Google Analytics 4 (GA4). While the previous version, Universal Analytics, had the option for using Client and Server side tracking (either or both in parallel), the GA4 destination offers only Server-side integration.<\/p>\n\n\n\n

GA4 Measurement Protocol<\/h3>\n\n\n\n

GA4 comes with a brand new API, aka Measurement Protocol, for measuring server-side events. This API, widely adopted for UA, allows marketers to send sensitive data to GA without exposing it on the client side. It also offered significantly more stable tracking for critical data such as e-commerce purchases.<\/p>\n\n\n\n

It wasn’t built to replace a full-blown client-side implementation of GA but rather only to enhance it.<\/p>\n\n\n\n

As a Google employee put it:<\/p>\n\n\n\n

\n

The Measurement Protocol for GA4 is only intended to supplement events already collected via other libraries such as the SDKs and gtag.js<\/p>\nSource: Google’s Issue Tracker<\/a><\/cite><\/blockquote>\n\n\n\n

Google Analytics Cookies<\/h3>\n\n\n\n

Another often overlooked issue with this setup is which anonymous identifier do we send as the Client ID (aka CID<\/em>). Segment defaults to using it own anonymous ID, which is a great solution for keeping your analytics platforms consistent. In a full server-side scenario, where no GA cookie is available it even makes sense.<\/p>\n\n\n\n

However, if you would later want to create audiences for ad targeting, these audiences won’t be able to match any user as the cookie wasn’t set by Google and can’t be traced back to a user.<\/p>\n\n\n

\n
\"image-2351002\"
Source: Google Analytics Support<\/a><\/figcaption><\/figure><\/div>\n\n\n

Getting it done right<\/h2>\n\n\n\n

So now we understand why using the standard GA4 destination is bad. I really hope they fix it soon enough so that we won’t have to jump through hoops to get it done right.<\/p>\n\n\n\n

Using Google Tag Manager<\/h3>\n\n\n\n

I’m a big fan of adding GTM on top of Segment. It might sound strange, but they work well together when each of the two is implemented to do exactly what it should.<\/p>\n\n\n

\n
\"image-2022806\"<\/figure><\/div>\n\n\n

The first step is to add GTM as a destination in Segment. By doing this we achieve two things:<\/p>\n\n\n\n

    \n
  1. The GTM code is loaded via Segment, no need for additional site edits<\/li>\n\n\n\n
  2. All the Segment event data is sent as DataLayer event data<\/li>\n<\/ol>\n\n\n\n

    Once we’ve set this up, every event logged by Segment can be used to create a Custom Event trigger for a GA4 event tag.<\/p>\n\n\n

    \n
    \"image-4616084\"<\/figure><\/div>\n\n\n

    In a simple scenario, multiple triggers can be combined under a single tag with the event’s name set dynamically from the Custom Event.<\/p>\n\n\n

    \n
    \"image-8647103\"<\/figure><\/div>\n\n\n

    A more complex scenario, with event parameters, will require mapping these specifically. This will require using multiple tags or some conditional logic.<\/p>\n\n\n\n

    Caveats to the Segment Datalayer push<\/h4>\n\n\n\n

    Segment’s events and parameters are specific to the event triggered, i.e. if a certain parameter is sent with a certain event, it won’t apply to subsequent events.<\/p>\n\n\n\n

    In the data layer, a parameter set in a certain key will persist throughout the same page (or until the data layer is reset<\/a>). This means that using a variable in GTM can represent data from a previous Segment push to the data layer, and not necessarily from the most recent event pushed.<\/p>\n","protected":false},"excerpt":{"rendered":"

    Over the past year, I’ve worked with almost a dozen different clients that migrated their Google Analytics implementation from UA to GA4 using Segment. While I’m generally fond of Segment and find implementing simple tags with it rather stable, its GA4 Destination is a mess. In this post, I’ll try to explain why it’s so […]<\/p>\n","protected":false},"author":1,"featured_media":5475,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,134],"tags":[],"class_list":["post-5461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-google-analytics","category-segment"],"_links":{"self":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5461"}],"collection":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/comments?post=5461"}],"version-history":[{"count":3,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5461\/revisions"}],"predecessor-version":[{"id":5469,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/posts\/5461\/revisions\/5469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media\/5475"}],"wp:attachment":[{"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/media?parent=5461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/categories?post=5461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trackingchef.com\/wp-json\/wp\/v2\/tags?post=5461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}