Skip to content

Commit 093cd6b

Browse files
jesus garcia ramirezjesus garcia ramirez
authored andcommitted
Properly handling test environment
1 parent 53f78bb commit 093cd6b

File tree

1 file changed

+67
-61
lines changed

1 file changed

+67
-61
lines changed

web/assets/js/widgets/core/snowplow-tracking.js

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,34 @@
101101
}
102102
};
103103

104-
// Helper function to get schema URL
105-
const getSchemaUrl = (eventType, environment) => {
106-
const schema = SCHEMA_CONFIG[environment][eventType];
107-
return `iglu:${schema.name}/jsonschema/${schema.version}`;
108-
};
109-
110104
const getEnvironment = () => {
111105
const apiUrl = WIDGET_SETTINGS.apiUrl;
112106
if (apiUrl.startsWith("https://projectaanvraag-api.uitdatabank.dev"))
113107
return {
114108
name: "dev",
115-
collector: "sneeuwploeg-dev.uitdatabank.be"
109+
collector: "sneeuwploeg-dev.uitdatabank.be",
110+
snowplowBackendEnvironment: "dev" // Maps to dev Snowplow backend
111+
};
112+
if (apiUrl.startsWith("https://projectaanvraag-api-test.uitdatabank.be"))
113+
return {
114+
name: "test",
115+
collector: "sneeuwploeg-dev.uitdatabank.be", // Use dev collector
116+
snowplowBackendEnvironment: "dev" // Test frontend maps to dev Snowplow backend
116117
};
117118
if (apiUrl.startsWith("https://projectaanvraag-api.uitdatabank.be"))
118119
return {
119120
name: "prod",
120121
collectors: [
121-
"sneeuwploeg.uitdatabank.be", // old collector
122+
"sneeuwploeg.uitdatabank.be", // old collector
122123
"sneeuwploeg-prd.uitdatabank.be" // new collector
123-
]
124+
],
125+
snowplowBackendEnvironment: "prod" // Maps to prod Snowplow backend
124126
};
125127
};
126128

127129
const environmentConfig = getEnvironment();
128130
const environment = environmentConfig.name;
131+
const snowplowBackendEnvironment = environmentConfig.snowplowBackendEnvironment;
129132

130133
const getTimeSpentInSeconds = () => {
131134
const endTime = new Date();
@@ -154,61 +157,51 @@
154157

155158
initializeSnowPlow(window, document, "script", SNOWPLOW_JS_URL, "widgetSnowplow");
156159

157-
// Initialize trackers based on environment
158-
if (environment === "dev") {
159-
window.widgetSnowplow(
160-
"newTracker",
161-
"widgets-tracker-dev",
162-
environmentConfig.collector,
163-
{
164-
appId: "widgets",
165-
platform: "web",
166-
cookieDomain: null,
167-
cookieName: "sppubliq",
168-
sessionCookieTimeout: 3600,
169-
discoverRootDomain: true,
170-
eventMethod: "post",
171-
encodeBase64: true,
172-
respectDoNotTrack: false,
173-
userFingerprint: true,
174-
postPath: "/publiq/t",
175-
contexts: {
176-
webPage: true,
177-
performanceTiming: false,
178-
gaCookies: true,
179-
geolocation: false,
180-
},
181-
}
182-
);
183-
} else {
184-
// Production environment - dual tracking
185-
environmentConfig.collectors.forEach((collector, index) => {
160+
// Common tracker configuration
161+
const getTrackerConfig = () => ({
162+
appId: "widgets",
163+
platform: "web",
164+
cookieDomain: null,
165+
cookieName: "sppubliq", // Same cookie name for all trackers
166+
sessionCookieTimeout: 3600,
167+
discoverRootDomain: true,
168+
eventMethod: "post",
169+
encodeBase64: true,
170+
respectDoNotTrack: false,
171+
userFingerprint: true,
172+
postPath: "/publiq/t",
173+
contexts: {
174+
webPage: true,
175+
performanceTiming: false,
176+
gaCookies: true,
177+
geolocation: false,
178+
}
179+
});
180+
181+
// Initialize tracker(s) based on environment
182+
const initializeTrackers = () => {
183+
if (environment === "prod") {
184+
// Production environment - dual tracking with identical configuration
185+
environmentConfig.collectors.forEach((collector, index) => {
186+
window.widgetSnowplow(
187+
"newTracker",
188+
`widgets-tracker-${index}`,
189+
collector,
190+
getTrackerConfig()
191+
);
192+
});
193+
} else {
194+
// Dev/Test environments - single tracker
186195
window.widgetSnowplow(
187196
"newTracker",
188-
`widgets-tracker-${index}`,
189-
collector,
190-
{
191-
appId: "widgets",
192-
platform: "web",
193-
cookieDomain: null,
194-
cookieName: `sppubliq`,
195-
sessionCookieTimeout: 3600,
196-
discoverRootDomain: true,
197-
eventMethod: "post",
198-
encodeBase64: true,
199-
respectDoNotTrack: false,
200-
userFingerprint: true,
201-
postPath: "/publiq/t",
202-
contexts: {
203-
webPage: true,
204-
performanceTiming: false,
205-
gaCookies: true,
206-
geolocation: false,
207-
},
208-
}
197+
`widgets-tracker-${environment}`, // Use specific environment name in tracker
198+
environmentConfig.collector,
199+
getTrackerConfig()
209200
);
210-
});
211-
}
201+
}
202+
};
203+
204+
initializeTrackers();
212205

213206
// Event data collectors by environment
214207
// These functions gather all required data for each event type in each environment
@@ -258,6 +251,10 @@
258251
environment,
259252
})
260253
},
254+
test: {
255+
// Test environment uses same collectors as dev
256+
...EVENT_DATA_COLLECTORS.dev
257+
},
261258
prod: {
262259
page_unload: () => {
263260
const timeSpent = getTimeSpentInSeconds();
@@ -349,6 +346,10 @@
349346
}
350347
})
351348
},
349+
test: {
350+
// Test environment uses same builders as dev
351+
...EVENT_BUILDERS.dev
352+
},
352353
prod: {
353354
page_unload: (data) => ({
354355
schema: getSchemaUrl('page_unload', 'prod'),
@@ -500,5 +501,10 @@
500501
trackerNames
501502
);
502503
});
504+
505+
const getSchemaUrl = (eventType) => {
506+
const schema = SCHEMA_CONFIG[snowplowBackendEnvironment][eventType];
507+
return `iglu:${schema.name}/jsonschema/${schema.version}`;
508+
};
503509
};
504510
})(CultuurnetWidgets);

0 commit comments

Comments
 (0)