plugeen 0.0.20 → 0.0.23
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/index.cjs +12 -8
- package/dist/index.js +12 -8
- package/dist/plugeen.global.js +2 -2
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -294,6 +294,8 @@ function initErrorsModule(http, logger) {
|
|
|
294
294
|
stack: event.error?.stack,
|
|
295
295
|
type: event.error?.name
|
|
296
296
|
}
|
|
297
|
+
}).catch(() => {
|
|
298
|
+
logger.warn("Failed to handle error");
|
|
297
299
|
});
|
|
298
300
|
});
|
|
299
301
|
window.addEventListener(
|
|
@@ -321,6 +323,8 @@ function initErrorsModule(http, logger) {
|
|
|
321
323
|
stack,
|
|
322
324
|
type: "UnhandledRejection"
|
|
323
325
|
}
|
|
326
|
+
}).catch(() => {
|
|
327
|
+
logger.warn("Failed to handle error");
|
|
324
328
|
});
|
|
325
329
|
}
|
|
326
330
|
);
|
|
@@ -470,14 +474,14 @@ function initWebVitals() {
|
|
|
470
474
|
const isBrowser = typeof window !== "undefined";
|
|
471
475
|
const hasPerformanceObserver = typeof PerformanceObserver !== "undefined";
|
|
472
476
|
if (!isBrowser || !hasPerformanceObserver) return [];
|
|
473
|
-
const metrics =
|
|
474
|
-
observeFCP((name, value) => metrics.
|
|
475
|
-
observeLCP((name, value) => metrics.
|
|
476
|
-
observeCLS((name, value) => metrics.
|
|
477
|
-
observeTTFB((name, value) => metrics.
|
|
478
|
-
observeINP((name, value) => metrics.
|
|
479
|
-
observeFPS((name, value) => metrics.
|
|
480
|
-
return
|
|
477
|
+
const metrics = [];
|
|
478
|
+
observeFCP((name, value) => metrics.push({ name, value }));
|
|
479
|
+
observeLCP((name, value) => metrics.push({ name, value }));
|
|
480
|
+
observeCLS((name, value) => metrics.push({ name, value }));
|
|
481
|
+
observeTTFB((name, value) => metrics.push({ name, value }));
|
|
482
|
+
observeINP((name, value) => metrics.push({ name, value }));
|
|
483
|
+
observeFPS((name, value) => metrics.push({ name, value }));
|
|
484
|
+
return metrics;
|
|
481
485
|
}
|
|
482
486
|
|
|
483
487
|
// src/modules/client/web-vitals/index.ts
|
package/dist/index.js
CHANGED
|
@@ -292,6 +292,8 @@ function initErrorsModule(http, logger) {
|
|
|
292
292
|
stack: event.error?.stack,
|
|
293
293
|
type: event.error?.name
|
|
294
294
|
}
|
|
295
|
+
}).catch(() => {
|
|
296
|
+
logger.warn("Failed to handle error");
|
|
295
297
|
});
|
|
296
298
|
});
|
|
297
299
|
window.addEventListener(
|
|
@@ -319,6 +321,8 @@ function initErrorsModule(http, logger) {
|
|
|
319
321
|
stack,
|
|
320
322
|
type: "UnhandledRejection"
|
|
321
323
|
}
|
|
324
|
+
}).catch(() => {
|
|
325
|
+
logger.warn("Failed to handle error");
|
|
322
326
|
});
|
|
323
327
|
}
|
|
324
328
|
);
|
|
@@ -468,14 +472,14 @@ function initWebVitals() {
|
|
|
468
472
|
const isBrowser = typeof window !== "undefined";
|
|
469
473
|
const hasPerformanceObserver = typeof PerformanceObserver !== "undefined";
|
|
470
474
|
if (!isBrowser || !hasPerformanceObserver) return [];
|
|
471
|
-
const metrics =
|
|
472
|
-
observeFCP((name, value) => metrics.
|
|
473
|
-
observeLCP((name, value) => metrics.
|
|
474
|
-
observeCLS((name, value) => metrics.
|
|
475
|
-
observeTTFB((name, value) => metrics.
|
|
476
|
-
observeINP((name, value) => metrics.
|
|
477
|
-
observeFPS((name, value) => metrics.
|
|
478
|
-
return
|
|
475
|
+
const metrics = [];
|
|
476
|
+
observeFCP((name, value) => metrics.push({ name, value }));
|
|
477
|
+
observeLCP((name, value) => metrics.push({ name, value }));
|
|
478
|
+
observeCLS((name, value) => metrics.push({ name, value }));
|
|
479
|
+
observeTTFB((name, value) => metrics.push({ name, value }));
|
|
480
|
+
observeINP((name, value) => metrics.push({ name, value }));
|
|
481
|
+
observeFPS((name, value) => metrics.push({ name, value }));
|
|
482
|
+
return metrics;
|
|
479
483
|
}
|
|
480
484
|
|
|
481
485
|
// src/modules/client/web-vitals/index.ts
|
package/dist/plugeen.global.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/* plugeen v0.0.
|
|
2
|
-
"use strict";var plugeen=(()=>{function E(){if(typeof document>"u")return null;let r=document.currentScript;if(!r){let o=document.getElementsByTagName("script");for(let i of Array.from(o))if(i.src&&(i.src.includes("/plugeen.global")||i.src.includes("/plugeen."))){r=i;break}}if(!r)return null;let e={},t=r.getAttribute("data-api-key")??void 0;e.apiUrl=r.getAttribute("data-api-url")??void 0,e.service=r.getAttribute("data-service")??void 0,e.analytics=r.getAttribute("data-analytics")==="true",e.webVitals=r.getAttribute("data-web-vitals")==="true",e.errors=r.getAttribute("data-errors")==="true";let n=r.getAttribute("data-debug");return n!==null&&(e.debug=n==="true"||n===""),Object.fromEntries(Object.entries({...e,apiKey:t}).filter(([o,i])=>i!=null&&i!==""))}function w(r){return r?{log:(...e)=>console.log("[plugeen]",...e),warn:(...e)=>console.warn("[plugeen]",...e),error:(...e)=>console.error("[plugeen]",...e)}:{log:()=>{},warn:()=>{},error:()=>{}}}function p(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();if(typeof crypto<"u"&&typeof crypto.getRandomValues=="function"){let r=new Uint8Array(16);crypto.getRandomValues(r),r[6]=r[6]&15|64,r[8]=r[8]&63|128;let e=Array.from(r).map(t=>t.toString(16).padStart(2,"0")).join("");return`${e.slice(0,8)}-${e.slice(8,12)}-${e.slice(12,16)}-${e.slice(16,20)}-${e.slice(20)}`}return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{let e=Math.floor(Math.random()*16);return(r==="x"?e:e&3|8).toString(16)})}var
|
|
1
|
+
/* plugeen v0.0.23 | https://plugeen.app */
|
|
2
|
+
"use strict";var plugeen=(()=>{function E(){if(typeof document>"u")return null;let r=document.currentScript;if(!r){let o=document.getElementsByTagName("script");for(let i of Array.from(o))if(i.src&&(i.src.includes("/plugeen.global")||i.src.includes("/plugeen."))){r=i;break}}if(!r)return null;let e={},t=r.getAttribute("data-api-key")??void 0;e.apiUrl=r.getAttribute("data-api-url")??void 0,e.service=r.getAttribute("data-service")??void 0,e.analytics=r.getAttribute("data-analytics")==="true",e.webVitals=r.getAttribute("data-web-vitals")==="true",e.errors=r.getAttribute("data-errors")==="true";let n=r.getAttribute("data-debug");return n!==null&&(e.debug=n==="true"||n===""),Object.fromEntries(Object.entries({...e,apiKey:t}).filter(([o,i])=>i!=null&&i!==""))}function w(r){return r?{log:(...e)=>console.log("[plugeen]",...e),warn:(...e)=>console.warn("[plugeen]",...e),error:(...e)=>console.error("[plugeen]",...e)}:{log:()=>{},warn:()=>{},error:()=>{}}}function p(){if(typeof crypto<"u"&&typeof crypto.randomUUID=="function")return crypto.randomUUID();if(typeof crypto<"u"&&typeof crypto.getRandomValues=="function"){let r=new Uint8Array(16);crypto.getRandomValues(r),r[6]=r[6]&15|64,r[8]=r[8]&63|128;let e=Array.from(r).map(t=>t.toString(16).padStart(2,"0")).join("");return`${e.slice(0,8)}-${e.slice(8,12)}-${e.slice(12,16)}-${e.slice(16,20)}-${e.slice(20)}`}return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,r=>{let e=Math.floor(Math.random()*16);return(r==="x"?e:e&3|8).toString(16)})}var g="plugeen_anon_id",m="plugeen_session_id",f="plugeen_session_ts";var P=18e5,c={};function I(){try{let r=localStorage.getItem(g);return r||(r=`anon_${p()}`,localStorage.setItem(g,r)),r}catch{return c[g]||(c[g]=`anon_${p()}`),c[g]}}function k(r){try{localStorage.setItem(g,r)}catch{c[g]=r}}function y(){try{let r=sessionStorage.getItem(m),e=sessionStorage.getItem(f);if(r&&e&&Date.now()-parseInt(e,10)<P)return sessionStorage.setItem(f,String(Date.now())),r;sessionStorage.removeItem(m),sessionStorage.removeItem(f);let t=`sess_${p()}`;return sessionStorage.setItem(m,t),sessionStorage.setItem(f,String(Date.now())),t}catch{let r=c[m],e=c[f];if(r&&e&&Date.now()-parseInt(e,10)<P)return c[f]=String(Date.now()),r;let t=`sess_${p()}`;return c[m]=t,c[f]=String(Date.now()),t}}var T=r=>new Promise(e=>setTimeout(e,r)),v=class{constructor(e,t,n){this.baseUrl=e;this.apiKey=t;this.logger=n}headers(e){let t={Authorization:`Bearer ${this.apiKey}`,"x-identity-id":I(),"x-session-id":y()};return e!=="GET"&&(t["Content-Type"]="application/json"),t}async request(e,{retries:t=0,method:n,body:o,searchParams:i},a=0){let u=i?`?${new URLSearchParams(i).toString()}`:"",l=`${this.baseUrl}${e}${u}`;this.logger.log(`[${n}] ${l}`);try{let s=await fetch(l,{method:n,headers:this.headers(n),credentials:"omit",body:o?JSON.stringify(o):void 0});if(s.status===401||s.status===404){let h=await s.json().catch(()=>null)||new Error(String(s.status));throw this.logger.error(`[${n}] ${l} \u2192 ${s.status}`,h),h}if((s.status>=500||s.status===429)&&t>0)return this.logger.warn(`[${n}] ${l} \u2192 ${s.status}, retrying (${t} left)`),await T((1+a)*1e3),this.request(e,{body:o,retries:t-1,method:n,searchParams:i},a+1);if(s.status>=200&&s.status<300){let d=await s.json().catch(()=>{throw new Error("Failed to parse json")});return this.logger.log(`[${n}] ${l} \u2192 ${s.status}`),d.data}throw new Error(`Unexpected status ${s.status}`)}catch(s){if(s instanceof TypeError&&t>0)return this.logger.warn(`[${n}] ${l} \u2192 network error, retrying (${t} left)`),await T(1e3),this.request(e,{body:o,method:n,retries:t-1,searchParams:i},a+1);throw this.logger.error(`[${n}] ${l} \u2192`,s),s}}async post(e,t,n){return this.request(e,{...n,retries:n?.retries??0,body:t,method:"POST"})}async get(e,t){return this.request(e,{...t,retries:t?.retries??0,method:"GET"})}beacon(e,t){if(typeof navigator>"u"||!navigator.sendBeacon)return!1;try{let n=new Blob([JSON.stringify(t)],{type:"application/json"});return navigator.sendBeacon(`${this.baseUrl}${e}`,n)}catch{return!1}}};function L(r,e){if(typeof window>"u")return;e.log("Analytics module started");let t=Date.now(),n=0,o=window.location.href,i=()=>{n++,r.post("/v1/analytics",{event:"page_view",url:window.location.href,title:document.title,sessionId:y(),referrer:document.referrer||void 0,screenWidth:window.innerWidth,screenHeight:window.innerHeight})},a=(s=!1)=>{let d={event:"page_exit",url:window.location.href,title:document.title,sessionId:y(),referrer:document.referrer||void 0,screenWidth:window.innerWidth,screenHeight:window.innerHeight,metadata:{timeOnPage:Math.round((Date.now()-t)/1e3),pageCount:n}};s?r.beacon("/v1/analytics",d):r.post("/v1/analytics",d)},u=()=>{window.location.href!==o&&(a(),t=Date.now(),o=window.location.href,i())},l=s=>{let d=history[s].bind(history);history[s]=(...h)=>{d(...h),u()}};l("pushState"),l("replaceState"),window.addEventListener("popstate",u),window.addEventListener("beforeunload",()=>a(!0)),document.addEventListener("visibilitychange",()=>document.visibilityState==="hidden"&&a(!0)),i()}var V=["chrome-extension://","moz-extension://","safari-extension://","edge-extension://"];function x(r){if(!r)return!1;let e=r.toLowerCase();return V.some(t=>e.includes(t))}function M(r,e){typeof window>"u"||(e.log("Errors module started"),window.addEventListener("error",t=>{x(t.filename)||x(t.error?.stack)||t.error===null&&t.message==="Script error."||r.post("/v1/logs",{message:t.message||"Unknown Error",level:"error",url:window.location.href,source:"client",metadata:{stack:t.error?.stack,type:t.error?.name}}).catch(()=>{e.warn("Failed to handle error")})}),window.addEventListener("unhandledrejection",t=>{let{reason:n}=t;if(x(n?.stack))return;let o="Unknown Error",i;n instanceof Error?(o=n.message,i=n.stack):typeof n=="string"?o=n:n!==null&&typeof n=="object"&&"message"in n&&(o=String(n.message)),r.post("/v1/logs",{message:o,url:window.location.href,source:"client",level:"error",metadata:{stack:i,type:"UnhandledRejection"}}).catch(()=>{e.warn("Failed to handle error")})}))}function S(){return performance.getEntriesByType("navigation")[0]?.activationStart??0}function H(){let r=performance.getEntriesByType("navigation")[0];if(r&&r.responseStart>0&&r.responseStart<performance.now())return r}function b(r,e,t){try{if(!PerformanceObserver.supportedEntryTypes.includes(r))return;let n=new PerformanceObserver(o=>{Promise.resolve().then(()=>e(o.getEntries()))});return n.observe({type:r,buffered:!0,...t??{}}),n}catch{return}}function R(r){let e=new Set;b("paint",t=>{for(let n of t)if(n.name==="first-contentful-paint"&&!e.has("first-contentful-paint")){e.add("first-contentful-paint");let o=Math.max(n.startTime-S(),0);r("first-contentful-paint",Math.round(o))}})}function D(r){let e=!1,t=b("largest-contentful-paint",o=>{if(e)return;let i=o[o.length-1];if(i){let a=Math.max(i.startTime-S(),0);r("largest-contentful-paint",Math.round(a))}});if(!t)return;let n=()=>{if(e)return;e=!0;let o=t.takeRecords();if(o.length>0){let i=o[o.length-1],a=Math.max(i.startTime-S(),0);r("largest-contentful-paint",Math.round(a))}t.disconnect()};for(let o of["keydown","click","visibilitychange"])addEventListener(o,n,{capture:!0,once:!0})}function N(r){let e=0,t=[],n=0;b("layout-shift",o=>{for(let i of o){let a=i;if(a.hadRecentInput)continue;let u=t[t.length-1],l=t[0];if(t.length>0&&u&&l&&a.startTime-u.startTime<1e3&&a.startTime-l.startTime<5e3?(e+=a.value,t.push(a)):(e=a.value,t=[a]),e>n){n=e;let s=Math.round(n*1e4)/1e4;r("cumulative-layout-shift",s)}}})}function j(r){let e=H();if(!e)return;let t=Math.max(e.responseStart-S(),0);r("time-to-first-byte",Math.round(t))}function W(r){let e=new Map,t=0;b("event",n=>{for(let o of n){let i=o;if(!i.interactionId)continue;let a=e.get(i.interactionId)??0;i.duration>a&&(e.set(i.interactionId,i.duration),i.duration>t&&(t=i.duration,r("interaction-to-next-paint",Math.round(i.duration))))}},{durationThreshold:40})}function B(r){if(typeof requestAnimationFrame>"u")return;let e=0,t=2e3,n=performance.now(),o=()=>{e++,performance.now()-n<t?requestAnimationFrame(o):r("frames-per-second",Math.round(e/t*1e3))};document.readyState==="complete"?requestAnimationFrame(o):window.addEventListener("load",()=>requestAnimationFrame(o),{once:!0})}function O(){if(!(typeof window<"u")||!(typeof PerformanceObserver<"u"))return[];let t=[];return R((n,o)=>t.push({name:n,value:o})),D((n,o)=>t.push({name:n,value:o})),N((n,o)=>t.push({name:n,value:o})),j((n,o)=>t.push({name:n,value:o})),W((n,o)=>t.push({name:n,value:o})),B((n,o)=>t.push({name:n,value:o})),t}function C(r,e){e.log("Web Vitals module started"),r.post("/v1/web-vitals",{url:window.location.href,metrics:O()})}function _(r,e){return e.log("Identities module started"),{async identify(t,n){let o=await r.post("/v1/identities",{...n,id:t});return k(t),o}}}function $(r,e){return e.log("Events module started"),{track:(t,n={})=>r.post("/v1/events",{name:t,data:n,source:"sdk"})}}function F(r,e){return e.log("Feature Flags module started"),{get:t=>r.get("/v1/feature-flags",{searchParams:{key:t}})}}function U(r,e){return e.log("Logs module started"),{send:t=>r.post("/v1/logs",t)}}function A(r,e){let t={...e,apiUrl:e.apiUrl??"https://www.plugeen.app/api",debug:e.debug??!1,analytics:e.analytics??!1,webVitals:e.webVitals??!1,errors:e.errors??!1},n=w(t.debug);if(!r)throw new TypeError("[plugeen] data-api-key is required");let o=new v(t.apiUrl,r,n),i={track:$(o,n).track,identify:_(o,n).identify,featureFlags:F(o,n),logs:U(o,n)};return typeof window<"u"&&(t.analytics&&L(o,n),t.webVitals&&C(o,n),t.errors&&M(o,n)),n.log(`Running on ${typeof window>"u"?"server":"client"} side`),i}function q(){let r=E(),e=w(!!r?.debug);if(r){if(!r.apiKey)return e.warn("Data-api-key is required.")}else return e.warn("Failed to start script");e.log("Started");let t=A(r.apiKey,r);return window.plugeen=t,t}window.plugeen||q();})();
|