diff --git a/app/assets/javascripts/tracking/tracking.js b/app/assets/javascripts/tracking/tracking.js index c26abc261edbd5306130029b8bfbdf34ced016e5..173eef0646bf0ab872ad8c46e41c16cc32baacd9 100644 --- a/app/assets/javascripts/tracking/tracking.js +++ b/app/assets/javascripts/tracking/tracking.js @@ -10,6 +10,8 @@ import { addReferrersCacheEntry, } from './utils'; +const ALLOWED_URL_HASHES = ['#diff', '#note']; + export default class Tracking { static queuedEvents = []; static initialized = false; @@ -183,7 +185,9 @@ export default class Tracking { originalUrl: window.location.href, }); - window.snowplow('setCustomUrl', pageLinks.url); + const appendHash = ALLOWED_URL_HASHES.some((prefix) => window.location.hash.startsWith(prefix)); + const customUrl = `${pageUrl}${appendHash ? window.location.hash : ''}`; + window.snowplow('setCustomUrl', customUrl); if (document.referrer) { const node = referrers.find((links) => links.originalUrl === document.referrer); diff --git a/spec/frontend/tracking/tracking_spec.js b/spec/frontend/tracking/tracking_spec.js index b7a2e4f4f51bbba2657337d83ae61b80145e5657..10380e6f4c522d3e256362768ba3c5dbdeb7c4bf 100644 --- a/spec/frontend/tracking/tracking_spec.js +++ b/spec/frontend/tracking/tracking_spec.js @@ -255,6 +255,23 @@ describe('Tracking', () => { expect(snowplowSpy).toHaveBeenCalledWith('setCustomUrl', TEST_HOST); }); + describe('allowed hashes/fragments', () => { + it.each` + hash | appends | description + ${'note_abc_123'} | ${true} | ${'appends'} + ${'diff-content-819'} | ${true} | ${'appends'} + ${'first_heading'} | ${false} | ${'does not append'} + `('$description `$hash` hash', ({ hash, appends }) => { + window.gl.snowplowPseudonymizedPageUrl = TEST_HOST; + window.location.hash = hash; + + Tracking.setAnonymousUrls(); + + const url = appends ? `${TEST_HOST}#${hash}` : TEST_HOST; + expect(snowplowSpy).toHaveBeenCalledWith('setCustomUrl', url); + }); + }); + it('does not set the referrer URL by default', () => { window.gl.snowplowPseudonymizedPageUrl = TEST_HOST;