UNPKG

tinyplg-browser

Version:

Browser analytics tracking library for tinyplg

125 lines (108 loc) 3.88 kB
(function () { var TRACKING_ENDPOINT = "https://server-4g8o.onrender.com"; var API_KEY = null; function getPageInfo() { return { url: window.location.href, referrer: document.referrer, userAgent: navigator.userAgent, language: navigator.language, }; } function sendTrackingData(data) { if (!API_KEY) { console.error( "API key is required. Please initialize the analytics with an API key." ); return; } fetch(TRACKING_ENDPOINT + "/api/pageview", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": API_KEY, }, body: JSON.stringify(data), }).catch((error) => { console.error("Error sending analytics data:", error); }); } function sendIdentifyData(userData) { if (!API_KEY) { console.error( "API key is required. Please initialize the analytics with an API key." ); return; } if (!userData.identification) { console.error( "Identification is required. Please provide an identification." ); return; } fetch(TRACKING_ENDPOINT + "/api/identify", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": API_KEY, }, body: JSON.stringify(userData), }).catch((error) => { console.error("Error sending identify data:", error); }); } function sendEventData(eventName, eventData) { if (!API_KEY) { console.error( "API key is required. Please initialize the analytics with an API key." ); return; } fetch(TRACKING_ENDPOINT + "/api/event", { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": API_KEY, }, body: JSON.stringify({ eventName, ...eventData }), }).catch((error) => { console.error("Error sending event data:", error); }); } window.analytics._init = function (apiKey, config) { API_KEY = apiKey; if (!API_KEY) { console.error("API key is required"); return; } sendTrackingData(getPageInfo()); let lastUrl = window.location.href; new MutationObserver(() => { const currentUrl = window.location.href; if (currentUrl !== lastUrl) { lastUrl = currentUrl; sendTrackingData(getPageInfo()); } }).observe(document, { subtree: true, childList: true }); }; window.analytics.identify = function (userData) { if (userData && typeof userData !== "object") { console.error("User data, if provided, must be an object."); return; } sendIdentifyData(userData || {}); }; window.analytics.funnel = function (eventName, eventData) { if (!eventName || typeof eventName !== "string") { console.error("Event name is required and must be a string."); return; } sendEventData(eventName, eventData || {}); }; if (window.analytics._i && window.analytics._i.length > 0) { var initData = window.analytics._i[0]; window.analytics._init(initData[0]); } })(); // snippet //<script>!(function(t,e){var o,n,p,r;e.__SV||(window.analytics=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)));}};((p=t.createElement("script")).type="text/javascript",p.async=!0,p.src="https://nickgros.github.io/browser/browser.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r));var u=e;for(void 0!==a?u=e[a]=[]:a="analytics",u.toString=function(t){var e="analytics";return"analytics"!==a&&(e+="."+a),t||(e+=" (stub)"),e},o="init track pageview".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a]);},e.__SV=1);})(document,window.analytics||[]);window.analytics.init('YOUR_API_KEY');</script>