From 5ca86f9183ad29bf19d1dbd4414c797a8ef1d83e Mon Sep 17 00:00:00 2001 From: ayesha waris <73840786+ayesha-waris@users.noreply.github.com> Date: Mon, 11 Nov 2024 17:17:58 +0500 Subject: [PATCH] perf: updated cohesion id stitching script (#1358) --- src/cohesion/index.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/cohesion/index.js b/src/cohesion/index.js index 68b02bb9..ab3f7e1c 100644 --- a/src/cohesion/index.js +++ b/src/cohesion/index.js @@ -39,18 +39,36 @@ const CohesionScript = () => { // Id Stitching script (executed after the cohesionScript is loaded) cohesionScript.onload = () => { idStitching.innerHTML = ` - window.tagular("beam", { - "@type": "core.Identify.v1", - traits: {}, - externalIds: [ - { - id: window.analytics.user().anonymousId(), - type: "segment_anonym_id", - collection: "users", - encoding: "none", - }, - ], - }); + cohesion("tagular:ready", function () { + window.analytics.ready(function () { + const cohesionAnonymId = window.tagular("getAliasSet")["anonymousId"]; + const segmentAnonymId = window.analytics.user().anonymousId(); + const segmentUserId = window.analytics.user().id(); + + window.analytics.identify(segmentUserId, { + cohesion_anonymous_id: cohesionAnonymId, + }); + + window.tagular("beam", { + "@type": "core.Identify.v1", + traits: {}, + externalIds: [ + { + id: segmentAnonymId, + type: "segment_anonymous_id", + collection: "users", + encoding: "none", + }, + { + id: cohesionAnonymId, + type: "cohesion_anonymous_id", + collection: "users", + encoding: "none", + }, + ], + }); + }); + }); `; document.head.appendChild(idStitching); };