1- import { collectAttribution , clearAttribution } from './attribution' ;
1+ import { collectSessionAttribution , collectPageAttribution , clearAttribution } from './attribution' ;
22
33const STORAGE_KEY = '__imtbl_attribution' ;
44
@@ -15,13 +15,13 @@ function setLocation(url: string) {
1515 } ) ;
1616}
1717
18- describe ( 'collectAttribution ' , ( ) => {
18+ describe ( 'collectSessionAttribution ' , ( ) => {
1919 it ( 'parses UTM parameters from the URL' , ( ) => {
2020 setLocation (
2121 'https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=spring&utm_content=banner&utm_term=nft' ,
2222 ) ;
2323
24- const result = collectAttribution ( ) ;
24+ const result = collectSessionAttribution ( ) ;
2525 expect ( result . utm_source ) . toBe ( 'google' ) ;
2626 expect ( result . utm_medium ) . toBe ( 'cpc' ) ;
2727 expect ( result . utm_campaign ) . toBe ( 'spring' ) ;
@@ -34,7 +34,7 @@ describe('collectAttribution', () => {
3434 'https://example.com/?gclid=abc&dclid=dc1&fbclid=fb2&ttclid=tt3&msclkid=ms4&li_fat_id=li5' ,
3535 ) ;
3636
37- const result = collectAttribution ( ) ;
37+ const result = collectSessionAttribution ( ) ;
3838 expect ( result . gclid ) . toBe ( 'abc' ) ;
3939 expect ( result . dclid ) . toBe ( 'dc1' ) ;
4040 expect ( result . fbclid ) . toBe ( 'fb2' ) ;
@@ -50,57 +50,57 @@ describe('collectAttribution', () => {
5050 configurable : true ,
5151 } ) ;
5252
53- const result = collectAttribution ( ) ;
53+ const result = collectSessionAttribution ( ) ;
5454 expect ( result . referrer ) . toBe ( 'https://google.com/search?q=nft' ) ;
5555 expect ( result . landing_page ) . toBe ( 'https://game.example.com/landing' ) ;
5656 } ) ;
5757
5858 it ( 'caches in sessionStorage and returns cached on second call' , ( ) => {
5959 setLocation ( 'https://example.com/?utm_source=google' ) ;
6060
61- const first = collectAttribution ( ) ;
61+ const first = collectSessionAttribution ( ) ;
6262 expect ( first . utm_source ) . toBe ( 'google' ) ;
6363
6464 // Change URL — should still return cached value
6565 setLocation ( 'https://example.com/?utm_source=facebook' ) ;
66- const second = collectAttribution ( ) ;
66+ const second = collectSessionAttribution ( ) ;
6767 expect ( second . utm_source ) . toBe ( 'google' ) ;
6868 } ) ;
6969
7070 it ( 'parses referral_code from the URL' , ( ) => {
7171 setLocation ( 'https://example.com/?referral_code=PARTNER42' ) ;
7272
73- const result = collectAttribution ( ) ;
73+ const result = collectSessionAttribution ( ) ;
7474 expect ( result . referral_code ) . toBe ( 'PARTNER42' ) ;
7575 } ) ;
7676
7777 it ( 'sets touchpoint_type to click when UTMs are present' , ( ) => {
7878 setLocation ( 'https://example.com/?utm_source=google' ) ;
7979
80- const result = collectAttribution ( ) ;
80+ const result = collectSessionAttribution ( ) ;
8181 expect ( result . touchpoint_type ) . toBe ( 'click' ) ;
8282 } ) ;
8383
8484 it ( 'sets touchpoint_type to click when a click ID is present' , ( ) => {
8585 setLocation ( 'https://example.com/?gclid=abc123' ) ;
8686
87- const result = collectAttribution ( ) ;
87+ const result = collectSessionAttribution ( ) ;
8888 expect ( result . touchpoint_type ) . toBe ( 'click' ) ;
8989 } ) ;
9090
9191 it ( 'does not set touchpoint_type when no UTMs or click IDs are present' , ( ) => {
9292 setLocation ( 'https://example.com/' ) ;
9393 Object . defineProperty ( document , 'referrer' , { value : 'https://other.com' , configurable : true } ) ;
9494
95- const result = collectAttribution ( ) ;
95+ const result = collectSessionAttribution ( ) ;
9696 expect ( result . touchpoint_type ) . toBeUndefined ( ) ;
9797 } ) ;
9898
9999 it ( 'returns empty attribution when no params are present' , ( ) => {
100100 setLocation ( 'https://example.com/' ) ;
101101 Object . defineProperty ( document , 'referrer' , { value : '' , configurable : true } ) ;
102102
103- const result = collectAttribution ( ) ;
103+ const result = collectSessionAttribution ( ) ;
104104 expect ( result . utm_source ) . toBeUndefined ( ) ;
105105 expect ( result . gclid ) . toBeUndefined ( ) ;
106106 expect ( result . referrer ) . toBeUndefined ( ) ;
@@ -115,15 +115,61 @@ describe('collectAttribution', () => {
115115 throw new Error ( 'storage disabled' ) ;
116116 } ) ;
117117
118- const result = collectAttribution ( ) ;
118+ const result = collectSessionAttribution ( ) ;
119119 expect ( result . utm_source ) . toBe ( 'twitter' ) ;
120120 } ) ;
121121} ) ;
122122
123+ describe ( 'collectPageAttribution' , ( ) => {
124+ it ( 'always parses from the current URL, ignoring sessionStorage' , ( ) => {
125+ setLocation ( 'https://example.com/?utm_source=google' ) ;
126+ collectSessionAttribution ( ) ; // seeds sessionStorage
127+
128+ // Change URL — collectSessionAttribution would return cached 'google',
129+ // but collectPageAttribution reads the new URL.
130+ setLocation ( 'https://example.com/?utm_source=facebook' ) ;
131+ const result = collectPageAttribution ( ) ;
132+ expect ( result . utm_source ) . toBe ( 'facebook' ) ;
133+ } ) ;
134+
135+ it ( 'does not write to sessionStorage' , ( ) => {
136+ setLocation ( 'https://example.com/?utm_source=twitter' ) ;
137+
138+ collectPageAttribution ( ) ;
139+ expect ( sessionStorage . getItem ( STORAGE_KEY ) ) . toBeNull ( ) ;
140+ } ) ;
141+
142+ it ( 'does not include landing_page' , ( ) => {
143+ setLocation ( 'https://example.com/?utm_source=google' ) ;
144+
145+ const result = collectPageAttribution ( ) ;
146+ expect ( result . utm_source ) . toBe ( 'google' ) ;
147+ expect ( result . landing_page ) . toBeUndefined ( ) ;
148+ } ) ;
149+
150+ it ( 'sets touchpoint_type to click when UTMs are present' , ( ) => {
151+ setLocation ( 'https://example.com/?utm_source=google' ) ;
152+
153+ const result = collectPageAttribution ( ) ;
154+ expect ( result . touchpoint_type ) . toBe ( 'click' ) ;
155+ } ) ;
156+
157+ it ( 'captures referrer' , ( ) => {
158+ setLocation ( 'https://example.com/' ) ;
159+ Object . defineProperty ( document , 'referrer' , {
160+ value : 'https://google.com/' ,
161+ configurable : true ,
162+ } ) ;
163+
164+ const result = collectPageAttribution ( ) ;
165+ expect ( result . referrer ) . toBe ( 'https://google.com/' ) ;
166+ } ) ;
167+ } ) ;
168+
123169describe ( 'clearAttribution' , ( ) => {
124170 it ( 'removes cached attribution from sessionStorage' , ( ) => {
125171 setLocation ( 'https://example.com/?utm_source=google' ) ;
126- collectAttribution ( ) ;
172+ collectSessionAttribution ( ) ;
127173 expect ( sessionStorage . getItem ( STORAGE_KEY ) ) . not . toBeNull ( ) ;
128174
129175 clearAttribution ( ) ;
0 commit comments