@affiliateo/web 1.4.0 → 1.5.1
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.
- package/dist/provider.js +29 -30
- package/package.json +1 -1
package/dist/provider.js
CHANGED
|
@@ -43,8 +43,6 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
|
|
|
43
43
|
});
|
|
44
44
|
const apiBase = (apiUrl || DEFAULT_API_URL).replace(/\/$/, '');
|
|
45
45
|
const campaignIdRef = useRef(campaignId);
|
|
46
|
-
const visitorIdRef = useRef(null);
|
|
47
|
-
const refRef = useRef(null);
|
|
48
46
|
const sentRef = useRef(false);
|
|
49
47
|
useEffect(() => {
|
|
50
48
|
if (typeof window === 'undefined')
|
|
@@ -56,26 +54,24 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
|
|
|
56
54
|
}
|
|
57
55
|
setCookie('affiliateo_visitor_id', vid, 365);
|
|
58
56
|
setCookie('affiliateo_campaign_id', campaignIdRef.current, 365);
|
|
59
|
-
visitorIdRef.current = vid;
|
|
60
57
|
// Read ref from URL params
|
|
61
58
|
const params = new URLSearchParams(window.location.search);
|
|
62
59
|
const ref = params.get('ref') || params.get('via') || params.get('source') || getCookie('affiliateo_ref') || null;
|
|
63
60
|
if (ref) {
|
|
64
61
|
setCookie('affiliateo_ref', ref, 365);
|
|
65
62
|
}
|
|
66
|
-
refRef.current = ref;
|
|
67
63
|
setState({ refCode: ref, visitorId: vid });
|
|
68
|
-
// Send
|
|
69
|
-
function
|
|
64
|
+
// Send pageview
|
|
65
|
+
function sendPageview() {
|
|
70
66
|
const payload = {
|
|
71
|
-
type,
|
|
67
|
+
type: 'pageview',
|
|
72
68
|
campaign_id: campaignIdRef.current,
|
|
73
|
-
visitor_id:
|
|
69
|
+
visitor_id: vid,
|
|
74
70
|
url: window.location.href,
|
|
75
71
|
path: window.location.pathname,
|
|
76
72
|
hostname: window.location.hostname,
|
|
77
73
|
referrer: document.referrer || '',
|
|
78
|
-
ref:
|
|
74
|
+
ref: ref || getCookie('affiliateo_ref') || '',
|
|
79
75
|
utm_source: params.get('utm_source') || '',
|
|
80
76
|
utm_medium: params.get('utm_medium') || '',
|
|
81
77
|
utm_campaign: params.get('utm_campaign') || '',
|
|
@@ -94,33 +90,36 @@ export function AffiliateoProvider({ campaignId, apiUrl, children, }) {
|
|
|
94
90
|
}).catch(() => { });
|
|
95
91
|
}
|
|
96
92
|
}
|
|
97
|
-
// Send
|
|
93
|
+
// Send pageview on mount (first load or page navigation)
|
|
98
94
|
if (!sentRef.current) {
|
|
99
95
|
sentRef.current = true;
|
|
100
|
-
|
|
96
|
+
sendPageview();
|
|
101
97
|
}
|
|
102
|
-
//
|
|
103
|
-
let
|
|
104
|
-
function
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
send('session_end');
|
|
109
|
-
}
|
|
110
|
-
function onVisibilityChange() {
|
|
111
|
-
if (document.visibilityState === 'hidden') {
|
|
112
|
-
onLeave();
|
|
113
|
-
}
|
|
114
|
-
else if (document.visibilityState === 'visible') {
|
|
115
|
-
left = false;
|
|
116
|
-
send('session_start');
|
|
98
|
+
// For SPA: listen for URL changes (popstate + pushState/replaceState)
|
|
99
|
+
let lastPath = window.location.pathname;
|
|
100
|
+
function onUrlChange() {
|
|
101
|
+
if (window.location.pathname !== lastPath) {
|
|
102
|
+
lastPath = window.location.pathname;
|
|
103
|
+
sendPageview();
|
|
117
104
|
}
|
|
118
105
|
}
|
|
119
|
-
|
|
120
|
-
window.addEventListener('
|
|
106
|
+
// Listen for browser back/forward
|
|
107
|
+
window.addEventListener('popstate', onUrlChange);
|
|
108
|
+
// Patch pushState/replaceState to detect SPA navigations
|
|
109
|
+
const origPushState = history.pushState;
|
|
110
|
+
const origReplaceState = history.replaceState;
|
|
111
|
+
history.pushState = function (...args) {
|
|
112
|
+
origPushState.apply(this, args);
|
|
113
|
+
onUrlChange();
|
|
114
|
+
};
|
|
115
|
+
history.replaceState = function (...args) {
|
|
116
|
+
origReplaceState.apply(this, args);
|
|
117
|
+
onUrlChange();
|
|
118
|
+
};
|
|
121
119
|
return () => {
|
|
122
|
-
|
|
123
|
-
|
|
120
|
+
window.removeEventListener('popstate', onUrlChange);
|
|
121
|
+
history.pushState = origPushState;
|
|
122
|
+
history.replaceState = origReplaceState;
|
|
124
123
|
};
|
|
125
124
|
}, [campaignId, apiBase]);
|
|
126
125
|
return (_jsx(AffiliateoContext.Provider, { value: state, children: children }));
|