If you use Segment's Google Analytics server-side integrations, even if you follow all of their documentation / recommendation, it is impossible to properly track your visitors/sessions in GA from anonymous through identified.
This means that if you use Segment like this, you cannot do very basic things in GA like understanding attribution of your product's sign ups. In GA, a brand new session is created for the identified users, which is not correct.
This problem applies to using Segment on the web with analytics.js when the Google Analytics Destination is set to Cloud Mode, or when using a true server-side Source such as Python/Ruby/Node.
This is a problem even if you carefully pass anonymousId
, userId
, web page URL/title, and user's IP from your frontend to your backend and then to Segment following all their recommendations.
In Segment's Google Analytics server-side integration they do this to compute the value passed to GA's cid
("Client ID") field:
let cid = hash(facade.userId() || facade.anonymousId())
The hash
they use is the string-hash npm package
But because they change this value as soon as a visitor is given a userId
, this makes it impossible to track users/sessions in GA from anonymous (marketing stie) to identified (signed up in product)
Segment incorrectly prefers sending a clientId
/cid
to Google Analytics based on the Segment userId
(if present) rather
than preferring their anonymousId
which would fix that problem and more appropriately use the fields outlined by
Google Analytics API documentation here
Segment needs to update their Google Analytics server-side integration to be like this:
let cid = hash(facade.anonymousId() || facade.userId())
This would allow for consistent tracking in GA from anonymous to identified. There is already a separate field/option to pass User ID directly to GA, so there's no reason to prefer userId
here when both are passed.
Segment has indicated that the more customers complain about this, the more likely they are to make this change. Currently they haven't committed to making any change :(
See code snippets below.
Segment recently shared:
Which should hopefully make this gist no longer needed.