|
| 1 | +import { collectAttribution, clearAttribution } from './attribution'; |
| 2 | + |
| 3 | +const STORAGE_KEY = '__imtbl_attribution'; |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + sessionStorage.clear(); |
| 7 | + jest.restoreAllMocks(); |
| 8 | +}); |
| 9 | + |
| 10 | +function setLocation(url: string) { |
| 11 | + Object.defineProperty(window, 'location', { |
| 12 | + value: new URL(url), |
| 13 | + writable: true, |
| 14 | + configurable: true, |
| 15 | + }); |
| 16 | +} |
| 17 | + |
| 18 | +describe('collectAttribution', () => { |
| 19 | + it('parses UTM parameters from the URL', () => { |
| 20 | + setLocation( |
| 21 | + 'https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=spring&utm_content=banner&utm_term=nft', |
| 22 | + ); |
| 23 | + |
| 24 | + const result = collectAttribution(); |
| 25 | + expect(result.utm_source).toBe('google'); |
| 26 | + expect(result.utm_medium).toBe('cpc'); |
| 27 | + expect(result.utm_campaign).toBe('spring'); |
| 28 | + expect(result.utm_content).toBe('banner'); |
| 29 | + expect(result.utm_term).toBe('nft'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('parses ad network click IDs', () => { |
| 33 | + setLocation( |
| 34 | + 'https://example.com/?gclid=abc123&fbclid=fb456&ttclid=tt789&msclkid=ms000', |
| 35 | + ); |
| 36 | + |
| 37 | + const result = collectAttribution(); |
| 38 | + expect(result.gclid).toBe('abc123'); |
| 39 | + expect(result.fbclid).toBe('fb456'); |
| 40 | + expect(result.ttclid).toBe('tt789'); |
| 41 | + expect(result.msclkid).toBe('ms000'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('captures referrer and landing page', () => { |
| 45 | + setLocation('https://game.example.com/landing'); |
| 46 | + Object.defineProperty(document, 'referrer', { |
| 47 | + value: 'https://google.com/search?q=nft', |
| 48 | + configurable: true, |
| 49 | + }); |
| 50 | + |
| 51 | + const result = collectAttribution(); |
| 52 | + expect(result.referrer).toBe('https://google.com/search?q=nft'); |
| 53 | + expect(result.landing_page).toBe('https://game.example.com/landing'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('caches in sessionStorage and returns cached on second call', () => { |
| 57 | + setLocation('https://example.com/?utm_source=google'); |
| 58 | + |
| 59 | + const first = collectAttribution(); |
| 60 | + expect(first.utm_source).toBe('google'); |
| 61 | + |
| 62 | + // Change URL — should still return cached value |
| 63 | + setLocation('https://example.com/?utm_source=facebook'); |
| 64 | + const second = collectAttribution(); |
| 65 | + expect(second.utm_source).toBe('google'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns empty attribution when no params are present', () => { |
| 69 | + setLocation('https://example.com/'); |
| 70 | + Object.defineProperty(document, 'referrer', { value: '', configurable: true }); |
| 71 | + |
| 72 | + const result = collectAttribution(); |
| 73 | + expect(result.utm_source).toBeUndefined(); |
| 74 | + expect(result.gclid).toBeUndefined(); |
| 75 | + expect(result.referrer).toBeUndefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('handles sessionStorage being unavailable', () => { |
| 79 | + setLocation('https://example.com/?utm_source=twitter'); |
| 80 | + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { |
| 81 | + throw new Error('storage disabled'); |
| 82 | + }); |
| 83 | + jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { |
| 84 | + throw new Error('storage disabled'); |
| 85 | + }); |
| 86 | + |
| 87 | + const result = collectAttribution(); |
| 88 | + expect(result.utm_source).toBe('twitter'); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('clearAttribution', () => { |
| 93 | + it('removes cached attribution from sessionStorage', () => { |
| 94 | + setLocation('https://example.com/?utm_source=google'); |
| 95 | + collectAttribution(); |
| 96 | + expect(sessionStorage.getItem(STORAGE_KEY)).not.toBeNull(); |
| 97 | + |
| 98 | + clearAttribution(); |
| 99 | + expect(sessionStorage.getItem(STORAGE_KEY)).toBeNull(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments