95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
/**
|
|
* Palantics main tracking script
|
|
*/
|
|
|
|
// Initialize tracking function
|
|
function tE(eventName, additionalParams = {}) {
|
|
// Create tracking data object
|
|
const data = {
|
|
event: eventName,
|
|
url: window.location.href,
|
|
ref: document.referrer,
|
|
ua: navigator.userAgent,
|
|
sw: window.screen.width,
|
|
lang: navigator.language || navigator.userLanguage,
|
|
};
|
|
|
|
// Add any additional parameters
|
|
Object.assign(data, additionalParams);
|
|
|
|
// Helper function to serialize an object into URL parameters
|
|
function serializeObject(obj, prefix) {
|
|
const str = [];
|
|
for (const p in obj) {
|
|
if (obj.hasOwnProperty(p)) {
|
|
const k = prefix ? prefix + "[" + p + "]" : p;
|
|
const v = obj[p];
|
|
if (v !== null && typeof v === "object") {
|
|
str.push(serializeObject(v, k));
|
|
} else {
|
|
str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v));
|
|
}
|
|
}
|
|
}
|
|
return str.join("&");
|
|
}
|
|
|
|
// Function for sending tracking data
|
|
function sendInfo() {
|
|
try {
|
|
// Get site URL and prepare REST API endpoint URL
|
|
const siteUrl = window.location.origin;
|
|
const restApiEndpoint = `${siteUrl}/wp-json/palantics/v1/sun`;
|
|
|
|
// Use POST request with data in body and query string
|
|
fetch(restApiEndpoint + '?' + serializeObject(data), {
|
|
method: "POST",
|
|
mode: "no-cors",
|
|
cache: "no-cache",
|
|
credentials: "omit", // Don't send cookies
|
|
headers: {
|
|
"X-Final-Destination": server, // Pass the final destination
|
|
"Content-Type": "application/json"
|
|
},
|
|
keepalive: true, // Ensures request completes even if page unloads
|
|
})
|
|
.catch(e => {
|
|
console.error("Tracking error:", e);
|
|
|
|
// If POST fails, try GET as fallback
|
|
fetch(restApiEndpoint + '?' + serializeObject(data), {
|
|
method: "GET",
|
|
mode: "no-cors",
|
|
cache: "no-cache",
|
|
credentials: "omit",
|
|
headers: {
|
|
"X-Final-Destination": server
|
|
},
|
|
keepalive: true,
|
|
}).catch(e2 => {
|
|
console.error("Fallback tracking error:", e2);
|
|
});
|
|
});
|
|
|
|
return true;
|
|
} catch (e) {
|
|
console.error("Tracking error:", e);
|
|
}
|
|
}
|
|
|
|
return sendInfo();
|
|
}
|
|
|
|
// Page load tracking function
|
|
function pL() {
|
|
tE("pageLoad_" + window.location.pathname);
|
|
}
|
|
|
|
// Set up page load tracking
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
try {
|
|
pL();
|
|
} catch (error) {
|
|
console.error("Error in page load tracking:", error);
|
|
}
|
|
}); |