Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/analytics/google.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export const googleDestination = 'G-2MDP3SDFL7';
const identityKey = 'expanso_legacy_ga_client';
const layerName = 'legacyAnalyticsLayer';
const eventNames = new Set(['$pageview', 'analytics_consent_updated', 'code_copy', 'search_used', 'page_not_found', 'outbound_click']);
const propertyNames = ['site_id', 'site_host', 'environment', 'analytics_schema_version', 'consent_state', 'identity_mode', 'analytics_test', 'is_internal', 'traffic_class', 'classification_version', 'search_length', 'result_count', 'destination_host', 'destination_path', 'link_placement'];
const sessionPropertyNames = ['site_id', 'site_host', 'environment', 'analytics_schema_version', 'consent_state', 'identity_mode', 'analytics_test', 'is_internal', 'traffic_class', 'classification_version'];
const propertyNames = [...sessionPropertyNames, 'search_length', 'result_count', 'destination_host', 'destination_path', 'link_placement'];
const consentSettings = state => ({analytics_storage: state === 'granted' ? 'granted' : 'denied', ad_storage: 'denied', ad_user_data: 'denied', ad_personalization: 'denied'});
function getIdentity(win) {
try {
Expand Down Expand Up @@ -38,8 +39,16 @@ export function createGoogleAdapter(win, initialConsent) {
gtag('set', 'url_passthrough', false);
gtag('js', new Date());
let initialized = false;
function sessionContext(properties) {
return {
...Object.fromEntries(sessionPropertyNames.filter(key => properties[key] !== undefined).map(key => [key, properties[key]])),
...(properties.analytics_test || properties.is_internal ? {debug_mode: true} : {}),
traffic_type: properties.is_internal ? 'internal' : properties.analytics_test ? 'analytics_test' : 'external',
};
}
function configuration(properties) {
return {
...sessionContext(properties),
send_page_view: false, client_id: identity, cookie_prefix: 'expanso_legacy', cookie_domain: 'none',
allow_google_signals: false, allow_ad_personalization_signals: false,
page_location: properties.$current_url, page_referrer: properties.$referrer || '',
Expand Down Expand Up @@ -74,8 +83,7 @@ export function createGoogleAdapter(win, initialConsent) {
...metadata, send_to: googleDestination, page_location: properties.$current_url,
page_referrer: properties.$referrer || '',
page_title: properties.site_id === 'legacy_docs' ? 'Legacy documentation' : 'Legacy website',
...(properties.analytics_test || properties.is_internal ? {debug_mode: true} : {}),
traffic_type: properties.is_internal ? 'internal' : properties.analytics_test ? 'analytics_test' : 'external',
...sessionContext(properties),
});
},
};
Expand Down
29 changes: 29 additions & 0 deletions src/analytics/google.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,36 @@ test('ordinary GA traffic omits debug_mode entirely instead of sending false', (
});
const payload = events(dom.window)[0][2];
assert.equal(Object.hasOwn(payload, 'debug_mode'), false);
const config = commands(dom.window).find(args => args[0] === 'config')[2];
assert.equal(Object.hasOwn(config, 'debug_mode'), false);
assert.equal(config.traffic_type, 'external');
assert.equal(config.page_location, payload.page_location);
assert.equal(config.page_referrer, payload.page_referrer);
assert.equal(payload.page_referrer, 'https://search.example');
assert.equal(payload.traffic_type, 'external');
dom.window.close();
});

test('destination config carries QA and internal context before SDK initialization and on consent changes', () => {
for (const internal of [false, true]) {
const {dom, win, collector} = setup();
if (internal) win.localStorage.setItem('expanso_analytics_internal', 'true');
collector.navigate();
collector.setConsent('granted');
collector.setConsent('denied');
const configs = commands(win).filter(args => args[0] === 'config');
assert.equal(configs.length, 3);
for (const [, destination, config] of configs) {
assert.equal(destination, googleDestination);
assert.equal(config.debug_mode, true);
assert.equal(config.analytics_test, true);
assert.equal(config.is_internal, internal);
assert.equal(config.traffic_type, internal ? 'internal' : 'analytics_test');
assert.equal(config.page_location, 'https://' + hosts[0] + '/');
assert.equal(config.page_referrer, '');
assert.equal(config.site_id, 'legacy_website');
assert.equal(config.send_page_view, false);
}
dom.window.close();
}
});