|
| 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=abc&dclid=dc1&fbclid=fb2&ttclid=tt3&msclkid=ms4&li_fat_id=li5', |
| 35 | + ); |
| 36 | + |
| 37 | + const result = collectAttribution(); |
| 38 | + expect(result.gclid).toBe('abc'); |
| 39 | + expect(result.dclid).toBe('dc1'); |
| 40 | + expect(result.fbclid).toBe('fb2'); |
| 41 | + expect(result.ttclid).toBe('tt3'); |
| 42 | + expect(result.msclkid).toBe('ms4'); |
| 43 | + expect(result.li_fat_id).toBe('li5'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('captures referrer and landing page', () => { |
| 47 | + setLocation('https://game.example.com/landing'); |
| 48 | + Object.defineProperty(document, 'referrer', { |
| 49 | + value: 'https://google.com/search?q=nft', |
| 50 | + configurable: true, |
| 51 | + }); |
| 52 | + |
| 53 | + const result = collectAttribution(); |
| 54 | + expect(result.referrer).toBe('https://google.com/search?q=nft'); |
| 55 | + expect(result.landing_page).toBe('https://game.example.com/landing'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('caches in sessionStorage and returns cached on second call', () => { |
| 59 | + setLocation('https://example.com/?utm_source=google'); |
| 60 | + |
| 61 | + const first = collectAttribution(); |
| 62 | + expect(first.utm_source).toBe('google'); |
| 63 | + |
| 64 | + // Change URL — should still return cached value |
| 65 | + setLocation('https://example.com/?utm_source=facebook'); |
| 66 | + const second = collectAttribution(); |
| 67 | + expect(second.utm_source).toBe('google'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('parses referral_code from the URL', () => { |
| 71 | + setLocation('https://example.com/?referral_code=PARTNER42'); |
| 72 | + |
| 73 | + const result = collectAttribution(); |
| 74 | + expect(result.referral_code).toBe('PARTNER42'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('sets touchpoint_type to click when UTMs are present', () => { |
| 78 | + setLocation('https://example.com/?utm_source=google'); |
| 79 | + |
| 80 | + const result = collectAttribution(); |
| 81 | + expect(result.touchpoint_type).toBe('click'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('sets touchpoint_type to click when a click ID is present', () => { |
| 85 | + setLocation('https://example.com/?gclid=abc123'); |
| 86 | + |
| 87 | + const result = collectAttribution(); |
| 88 | + expect(result.touchpoint_type).toBe('click'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not set touchpoint_type when no UTMs or click IDs are present', () => { |
| 92 | + setLocation('https://example.com/'); |
| 93 | + Object.defineProperty(document, 'referrer', { value: 'https://other.com', configurable: true }); |
| 94 | + |
| 95 | + const result = collectAttribution(); |
| 96 | + expect(result.touchpoint_type).toBeUndefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('returns empty attribution when no params are present', () => { |
| 100 | + setLocation('https://example.com/'); |
| 101 | + Object.defineProperty(document, 'referrer', { value: '', configurable: true }); |
| 102 | + |
| 103 | + const result = collectAttribution(); |
| 104 | + expect(result.utm_source).toBeUndefined(); |
| 105 | + expect(result.gclid).toBeUndefined(); |
| 106 | + expect(result.referrer).toBeUndefined(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('handles sessionStorage being unavailable', () => { |
| 110 | + setLocation('https://example.com/?utm_source=twitter'); |
| 111 | + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { |
| 112 | + throw new Error('storage disabled'); |
| 113 | + }); |
| 114 | + jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { |
| 115 | + throw new Error('storage disabled'); |
| 116 | + }); |
| 117 | + |
| 118 | + const result = collectAttribution(); |
| 119 | + expect(result.utm_source).toBe('twitter'); |
| 120 | + }); |
| 121 | +}); |
| 122 | + |
| 123 | +describe('clearAttribution', () => { |
| 124 | + it('removes cached attribution from sessionStorage', () => { |
| 125 | + setLocation('https://example.com/?utm_source=google'); |
| 126 | + collectAttribution(); |
| 127 | + expect(sessionStorage.getItem(STORAGE_KEY)).not.toBeNull(); |
| 128 | + |
| 129 | + clearAttribution(); |
| 130 | + expect(sessionStorage.getItem(STORAGE_KEY)).toBeNull(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments