adding GA4 properties to Segment analytics events (#1253)

This commit is contained in:
jasonbryant84
2024-11-04 09:29:48 +00:00
committed by GitHub
parent 01529c6777
commit 07d0b360c3
4 changed files with 17 additions and 2 deletions
@@ -98,3 +98,9 @@ export function tagCloudUILinksWithAnonymousId() {
link.href = addOrUpdateQueryParam(link.href, CROSS_SITE_URL_PARAM_KEY, anonymousId);
});
}
export function addGA4Properties(properties) {
const gaMeasurementId = getCookie('ga_measurement_id')?.replace('G-', '');
properties.ga_session_id = getCookie('_ga_' + gaMeasurementId)?.replace('GS1.1.','').split('.')[0];
properties.ga_client_id = getCookie('_ga')?.replace('GA1.1.','');
}
@@ -1,4 +1,4 @@
import { getCookie, devLog, tagCloudUILinksWithAnonymousId } from './helpers';
import { addGA4Properties, getCookie, devLog, tagCloudUILinksWithAnonymousId } from './helpers';
const WRITE_KEY = 'segmentWriteKey';
const PAGES_SESSION_STORAGE_KEY = 'segmentPages';
@@ -125,6 +125,8 @@ const trackStoredPageViews = () => {
const originalTimestamp = properties.storedEvent ? properties.storedTimestamp : null;
delete properties['storedTimestamp'];
addGA4Properties(properties);
if(window.analytics) {
window.analytics.page(
category,
@@ -151,6 +153,8 @@ const trackEvent = (name, properties = {}) => {
const originalTimestamp = properties.storedEvent ? properties.storedTimestamp : null;
delete properties['storedTimestamp'];
addGA4Properties(properties);
if(window.analytics) {
window.analytics.track({
event: name,
@@ -1,6 +1,11 @@
import * as params from '@params';
import { setCookie } from './helpers';
import { setSegmentWriteKey } from './segment-helpers';
if (params.segmentWriteKey) {
setSegmentWriteKey(params.segmentWriteKey);
}
if (params.gaMeasurementId) {
setCookie('ga_measurement_id', params.gaMeasurementId, 365);
}