@affiliateo/web 1.4.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.
Files changed (2) hide show
  1. package/dist/provider.js +33 -11
  2. package/package.json +1 -1
package/dist/provider.js CHANGED
@@ -94,33 +94,55 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
94
94
  }).catch(() => { });
95
95
  }
96
96
  }
97
- // Send initial session_start
98
- 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) {
99
101
  sentRef.current = true;
100
102
  send('session_start');
101
103
  }
102
- // Visibility change — session_start/session_end
103
- 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;
104
109
  function onLeave() {
105
- if (left)
110
+ if (ended)
106
111
  return;
107
- left = true;
112
+ ended = true;
108
113
  send('session_end');
109
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;
110
118
  function onVisibilityChange() {
111
119
  if (document.visibilityState === 'hidden') {
112
- 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);
113
125
  }
114
126
  else if (document.visibilityState === 'visible') {
115
- left = false;
116
- 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);
117
138
  }
118
139
  }
119
140
  document.addEventListener('visibilitychange', onVisibilityChange);
120
- window.addEventListener('beforeunload', onLeave);
121
141
  return () => {
122
- document.removeEventListener('visibilitychange', onVisibilityChange);
123
142
  window.removeEventListener('beforeunload', onLeave);
143
+ document.removeEventListener('visibilitychange', onVisibilityChange);
144
+ if (hiddenTimer)
145
+ clearTimeout(hiddenTimer);
124
146
  };
125
147
  }, [campaignId, apiBase]);
126
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.4.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",