|
| 1 | +import { parseAttribution } from './attribution'; |
| 2 | + |
| 3 | +const originalLocation = window.location; |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + sessionStorage.clear(); |
| 7 | +}); |
| 8 | + |
| 9 | +afterEach(() => { |
| 10 | + Object.defineProperty(window, 'location', { |
| 11 | + value: originalLocation, |
| 12 | + writable: true, |
| 13 | + configurable: true, |
| 14 | + }); |
| 15 | + Object.defineProperty(document, 'referrer', { |
| 16 | + value: '', |
| 17 | + configurable: true, |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('parseAttribution', () => { |
| 22 | + it('parses UTM params from the URL', () => { |
| 23 | + Object.defineProperty(window, 'location', { |
| 24 | + value: { |
| 25 | + search: '?utm_source=youtube&utm_medium=influencer&utm_campaign=launch', |
| 26 | + href: 'https://studio.com/shop?utm_source=youtube&utm_medium=influencer&utm_campaign=launch', |
| 27 | + }, |
| 28 | + writable: true, |
| 29 | + configurable: true, |
| 30 | + }); |
| 31 | + |
| 32 | + const ctx = parseAttribution(); |
| 33 | + expect(ctx.utm_source).toBe('youtube'); |
| 34 | + expect(ctx.utm_medium).toBe('influencer'); |
| 35 | + expect(ctx.utm_campaign).toBe('launch'); |
| 36 | + expect(ctx.landing_page).toBe(window.location.href); |
| 37 | + }); |
| 38 | + |
| 39 | + it('parses click IDs from the URL', () => { |
| 40 | + Object.defineProperty(window, 'location', { |
| 41 | + value: { |
| 42 | + search: '?gclid=abc&fbclid=def&ttclid=ghi&msclkid=jkl&dclid=mno&li_fat_id=pqr', |
| 43 | + href: 'https://studio.com/?gclid=abc&fbclid=def&ttclid=ghi&msclkid=jkl&dclid=mno&li_fat_id=pqr', |
| 44 | + }, |
| 45 | + writable: true, |
| 46 | + configurable: true, |
| 47 | + }); |
| 48 | + |
| 49 | + const ctx = parseAttribution(); |
| 50 | + expect(ctx.gclid).toBe('abc'); |
| 51 | + expect(ctx.fbclid).toBe('def'); |
| 52 | + expect(ctx.ttclid).toBe('ghi'); |
| 53 | + expect(ctx.msclkid).toBe('jkl'); |
| 54 | + expect(ctx.dclid).toBe('mno'); |
| 55 | + expect(ctx.li_fat_id).toBe('pqr'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('parses ref param as referral_code', () => { |
| 59 | + Object.defineProperty(window, 'location', { |
| 60 | + value: { |
| 61 | + search: '?ref=creator_handle', |
| 62 | + href: 'https://studio.com/?ref=creator_handle', |
| 63 | + }, |
| 64 | + writable: true, |
| 65 | + configurable: true, |
| 66 | + }); |
| 67 | + |
| 68 | + const ctx = parseAttribution(); |
| 69 | + expect(ctx.referral_code).toBe('creator_handle'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('returns cached attribution on subsequent calls within session', () => { |
| 73 | + Object.defineProperty(window, 'location', { |
| 74 | + value: { |
| 75 | + search: '?utm_source=first', |
| 76 | + href: 'https://studio.com/?utm_source=first', |
| 77 | + }, |
| 78 | + writable: true, |
| 79 | + configurable: true, |
| 80 | + }); |
| 81 | + |
| 82 | + const first = parseAttribution(); |
| 83 | + expect(first.utm_source).toBe('first'); |
| 84 | + |
| 85 | + // Change URL (simulating SPA navigation) |
| 86 | + Object.defineProperty(window, 'location', { |
| 87 | + value: { |
| 88 | + search: '', |
| 89 | + href: 'https://studio.com/shop', |
| 90 | + }, |
| 91 | + writable: true, |
| 92 | + configurable: true, |
| 93 | + }); |
| 94 | + |
| 95 | + const second = parseAttribution(); |
| 96 | + expect(second.utm_source).toBe('first'); // Still the original |
| 97 | + }); |
| 98 | + |
| 99 | + it('captures document.referrer', () => { |
| 100 | + Object.defineProperty(document, 'referrer', { |
| 101 | + value: 'https://google.com/search?q=immutable', |
| 102 | + configurable: true, |
| 103 | + }); |
| 104 | + Object.defineProperty(window, 'location', { |
| 105 | + value: { |
| 106 | + search: '', |
| 107 | + href: 'https://studio.com/', |
| 108 | + }, |
| 109 | + writable: true, |
| 110 | + configurable: true, |
| 111 | + }); |
| 112 | + |
| 113 | + const ctx = parseAttribution(); |
| 114 | + expect(ctx.referrer).toBe('https://google.com/search?q=immutable'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('only includes params that are present', () => { |
| 118 | + Object.defineProperty(window, 'location', { |
| 119 | + value: { |
| 120 | + search: '', |
| 121 | + href: 'https://studio.com/', |
| 122 | + }, |
| 123 | + writable: true, |
| 124 | + configurable: true, |
| 125 | + }); |
| 126 | + |
| 127 | + const ctx = parseAttribution(); |
| 128 | + expect(ctx.utm_source).toBeUndefined(); |
| 129 | + expect(ctx.gclid).toBeUndefined(); |
| 130 | + expect(ctx.landing_page).toBe('https://studio.com/'); |
| 131 | + }); |
| 132 | +}); |
0 commit comments