diff --git a/metadata.yaml b/metadata.yaml index 620b1a2..6fa38af 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -1,6 +1,8 @@ homepage: 'https://stape.io/' documentation: 'https://stape.io/blog/linkedin-conversion-api-tag-for-server-google-tag-manager' versions: + - sha: 7cd6168e4637a922c4e71f3c64fe47fc2fb20784 + changeNotes: Added support for li_fat_id cookie from Data Tag common_cookie. - sha: 3686f8f16ba42e7430ea7c11bf929625aea3718d changeNotes: Updated consent section. Updated API version to 202601. - sha: 94000fb78f50b2442788a620ae34628cfbf16910 diff --git a/template.js b/template.js index f70f1db..52e2f2f 100644 --- a/template.js +++ b/template.js @@ -22,7 +22,6 @@ const sha256Sync = require('sha256Sync'); const isLoggingEnabled = determinateIsLoggingEnabled(); const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined; const cookieName = 'li_fat_id'; - const eventData = getAllEventData(); if (!isConsentGivenOrNotRequired()) { @@ -30,12 +29,11 @@ if (!isConsentGivenOrNotRequired()) { } if (data.type === 'page_view') { + const clickId = getLinkedInFirstPartyAdsTrackingUuid(); const url = eventData.page_location || getRequestHeader('referer'); if (url) { - const value = parseUrl(url).searchParams[cookieName]; - - if (value) { + if (clickId) { const options = { domain: 'auto', path: '/', @@ -44,7 +42,7 @@ if (data.type === 'page_view') { 'max-age': 86400 * 90 }; - setCookie(cookieName, value, options, false); + setCookie(cookieName, clickId, options, false); } } @@ -239,8 +237,16 @@ function getUserEmail() { } function getLinkedInFirstPartyAdsTrackingUuid() { - const liFatId = decodeUriComponent(getCookieValues(cookieName)[0] || ''); - return liFatId || userIdsOverride.linkedinFirstPartyId || user_data.linkedinFirstPartyId || ''; + const commonCookie = eventData.common_cookie || {}; + return ( + parseClickIdFromUrl(eventData, cookieName) || + getCookieValues(cookieName)[0] || + commonCookie[cookieName] || + eventData[cookieName] || + userIdsOverride.linkedinFirstPartyId || + user_data.linkedinFirstPartyId || + '' + ); } function getAcxiomId() { @@ -322,6 +328,13 @@ function getUserInfo() { /*============================================================================== Helpers ==============================================================================*/ +function parseClickIdFromUrl(eventData, cookieName) { + const url = eventData.page_location || eventData.page_referrer || getRequestHeader('referer'); + if (!url) return; + + const urlSearchParams = parseUrl(url).searchParams; + return urlSearchParams[cookieName]; +} function isHashed(value) { if (!value) { diff --git a/template.tpl b/template.tpl index 755a1e7..eebfbb9 100644 --- a/template.tpl +++ b/template.tpl @@ -350,7 +350,6 @@ const sha256Sync = require('sha256Sync'); const isLoggingEnabled = determinateIsLoggingEnabled(); const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined; const cookieName = 'li_fat_id'; - const eventData = getAllEventData(); if (!isConsentGivenOrNotRequired()) { @@ -358,12 +357,11 @@ if (!isConsentGivenOrNotRequired()) { } if (data.type === 'page_view') { + const clickId = getLinkedInFirstPartyAdsTrackingUuid(); const url = eventData.page_location || getRequestHeader('referer'); if (url) { - const value = parseUrl(url).searchParams[cookieName]; - - if (value) { + if (clickId) { const options = { domain: 'auto', path: '/', @@ -372,7 +370,7 @@ if (data.type === 'page_view') { 'max-age': 86400 * 90 }; - setCookie(cookieName, value, options, false); + setCookie(cookieName, clickId, options, false); } } @@ -567,8 +565,16 @@ function getUserEmail() { } function getLinkedInFirstPartyAdsTrackingUuid() { - const liFatId = decodeUriComponent(getCookieValues(cookieName)[0] || ''); - return liFatId || userIdsOverride.linkedinFirstPartyId || user_data.linkedinFirstPartyId || ''; + const commonCookie = eventData.common_cookie || {}; + return ( + parseClickIdFromUrl(eventData, cookieName) || + getCookieValues(cookieName)[0] || + commonCookie[cookieName] || + eventData[cookieName] || + userIdsOverride.linkedinFirstPartyId || + user_data.linkedinFirstPartyId || + '' + ); } function getAcxiomId() { @@ -650,6 +656,13 @@ function getUserInfo() { /*============================================================================== Helpers ==============================================================================*/ +function parseClickIdFromUrl(eventData, cookieName) { + const url = eventData.page_location || eventData.page_referrer || getRequestHeader('referer'); + if (!url) return; + + const urlSearchParams = parseUrl(url).searchParams; + return urlSearchParams[cookieName]; +} function isHashed(value) { if (!value) {