Discussions

Provide Session IDs to Advanced Buyer Analytics

How to populate the client_provided_id column in Threekit's Advanced Buyer Analytics.

Introduction

If you are utilizing Advanced Buyer Analytics, you may want a way to associate the sessions recorded by Threekit with analytics data you are recording and gathering in other tools or platforms.

In Threekit's Advanced Buyer Analytics output there is a column called client_provided_id that customers can pass a Google Analytics Session ID. This will allow you to tie that client_provided_id to session_id in the output.

To provide this custom ID to Threekit and Advanced Buyer Analytics, you must populate the optional embed parameter analyticsCustomId. For example:

window
    .threekitPlayer({
        authToken: "01234567-89ab-cdef-0123-456789abcdef",
        el: document.getElementById("player"),
        assetId: "e12a45f7-8b39-cd06-e12a-45f78b39cd06", 
        showConfigurator: true,
        analyticsCustomId: '4b607cc386b', // ID from analytics tool
    })
    .then(async function(player) {
        window.player = player;
    });

Finding Google Analytics's Session ID

This guide is operating under the assumption that the analytics tool being used is Google Analytics (GA4). We have sourced some information on gathering our Session ID from the Tracking Chef article.

Universal analytics was based on sessions, and once you passed the ga cookie id (i.e. client id) in the measurement protocol hit, then GA would match that hit to its corresponding “place”, but GA4 is built on events and not sessions, and this is why we pass to “tell” GA the session_id.

When a session_start event is fired a new session_id is created by GA, but it is not part of the _ga cookie like it was in Universal, it’s part of a new cookie that contains the container_id

And looks something like this: _ga_XXXXXXXXXX, where the X’s stand for the stream id. To get the Session ID, you should be able to use a script such as:

function getSessionNumber() {
// replace ABCDEFG with your GA Stream ID
  const pattern = /_ga_ABCDEFG=GSd.d.(w+).(d+)/;
  const match = document.cookie.match(pattern);
  const sessionNumber = match?.[2];

  if (!sessionNumber) {
    // Cookie not yet available; wait a bit and try again.
    window.setTimeout(() => getSessionNumber(callback), 200);
    return;
  }

  return sessionNumber;
}

If you're using GTM, this script may work better:

function getSessionId() {
  var pattern = /_ga_0J0X8GBMJE=GSd.d.(w+).(d+)/;
  var match = document.cookie.match(pattern);
  var sessionId = match && match[1];

  if (!sessionId) {
    return;
  }

  return sessionId;
}