@affiliateo/web 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,4 +17,5 @@ export declare function getMetadata(req: {
17
17
  }): {
18
18
  affiliateo_visitor_id?: string;
19
19
  affiliateo_ref?: string;
20
+ campaign_id?: string;
20
21
  };
package/dist/metadata.js CHANGED
@@ -11,10 +11,13 @@ export function getMetadata(req) {
11
11
  if (req.cookies && typeof req.cookies === 'object' && 'get' in req.cookies && typeof req.cookies.get === 'function') {
12
12
  const visitorId = req.cookies.get('affiliateo_visitor_id');
13
13
  const ref = req.cookies.get('affiliateo_ref');
14
+ const campaignId = req.cookies.get('affiliateo_campaign_id');
14
15
  if (visitorId?.value)
15
16
  metadata.affiliateo_visitor_id = visitorId.value;
16
17
  if (ref?.value)
17
18
  metadata.affiliateo_ref = ref.value;
19
+ if (campaignId?.value)
20
+ metadata.campaign_id = campaignId.value;
18
21
  return metadata;
19
22
  }
20
23
  // Try plain object cookies (Express, Pages Router, etc.)
@@ -24,6 +27,8 @@ export function getMetadata(req) {
24
27
  metadata.affiliateo_visitor_id = cookies.affiliateo_visitor_id;
25
28
  if (cookies.affiliateo_ref)
26
29
  metadata.affiliateo_ref = cookies.affiliateo_ref;
30
+ if (cookies.affiliateo_campaign_id)
31
+ metadata.campaign_id = cookies.affiliateo_campaign_id;
27
32
  return metadata;
28
33
  }
29
34
  // Fallback: parse cookie header manually
@@ -36,6 +41,8 @@ export function getMetadata(req) {
36
41
  metadata.affiliateo_visitor_id = cookies.affiliateo_visitor_id;
37
42
  if (cookies.affiliateo_ref)
38
43
  metadata.affiliateo_ref = cookies.affiliateo_ref;
44
+ if (cookies.affiliateo_campaign_id)
45
+ metadata.campaign_id = cookies.affiliateo_campaign_id;
39
46
  }
40
47
  return metadata;
41
48
  }
package/dist/provider.js CHANGED
@@ -55,6 +55,7 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
55
55
  vid = uuid();
56
56
  }
57
57
  setCookie('affiliateo_visitor_id', vid, 365);
58
+ setCookie('affiliateo_campaign_id', campaignIdRef.current, 365);
58
59
  visitorIdRef.current = vid;
59
60
  // Read ref from URL params
60
61
  const params = new URLSearchParams(window.location.search);
@@ -93,33 +94,55 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
93
94
  }).catch(() => { });
94
95
  }
95
96
  }
96
- // Send initial session_start
97
- if (!sentRef.current) {
97
+ // Session cookie — 30 min expiry. Only send session_start if no active session.
98
+ const SESSION_COOKIE = 'affiliateo_session';
99
+ const hasActiveSession = !!getCookie(SESSION_COOKIE);
100
+ if (!hasActiveSession && !sentRef.current) {
98
101
  sentRef.current = true;
99
102
  send('session_start');
100
103
  }
101
- // Visibility change — session_start/session_end
102
- let left = false;
104
+ // Refresh session cookie (30 min sliding window)
105
+ setCookie(SESSION_COOKIE, '1', 1 / 48); // 30 minutes = 1/48 of a day
106
+ // session_end only fires when actually leaving the site (tab close, navigate away)
107
+ // NOT on SPA page navigations or tab switching
108
+ let ended = false;
103
109
  function onLeave() {
104
- if (left)
110
+ if (ended)
105
111
  return;
106
- left = true;
112
+ ended = true;
107
113
  send('session_end');
108
114
  }
115
+ window.addEventListener('beforeunload', onLeave);
116
+ // On visibility change: refresh session cookie when visible, send session_end after 30 min hidden
117
+ let hiddenTimer = null;
109
118
  function onVisibilityChange() {
110
119
  if (document.visibilityState === 'hidden') {
111
- onLeave();
120
+ // Start a 30-min timer — if user doesn't come back, session is over
121
+ hiddenTimer = setTimeout(() => {
122
+ send('session_end');
123
+ ended = true;
124
+ }, 30 * 60 * 1000);
112
125
  }
113
126
  else if (document.visibilityState === 'visible') {
114
- left = false;
115
- send('session_start');
127
+ // User came back — cancel the timer, refresh session cookie
128
+ if (hiddenTimer) {
129
+ clearTimeout(hiddenTimer);
130
+ hiddenTimer = null;
131
+ }
132
+ // If session expired while away (30+ min), start a new one
133
+ if (!getCookie(SESSION_COOKIE)) {
134
+ ended = false;
135
+ send('session_start');
136
+ }
137
+ setCookie(SESSION_COOKIE, '1', 1 / 48);
116
138
  }
117
139
  }
118
140
  document.addEventListener('visibilitychange', onVisibilityChange);
119
- window.addEventListener('beforeunload', onLeave);
120
141
  return () => {
121
- document.removeEventListener('visibilitychange', onVisibilityChange);
122
142
  window.removeEventListener('beforeunload', onLeave);
143
+ document.removeEventListener('visibilitychange', onVisibilityChange);
144
+ if (hiddenTimer)
145
+ clearTimeout(hiddenTimer);
123
146
  };
124
147
  }, [campaignId, apiBase]);
125
148
  return (_jsx(AffiliateoContext.Provider, { value: state, children: children }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@affiliateo/web",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Affiliateo web SDK — affiliate attribution and session tracking for web apps",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",