try out script tag

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-02-05 22:09:07 +01:00
parent ceee19bc36
commit fb18e49078
5 changed files with 14 additions and 10 deletions

View File

@@ -38,7 +38,7 @@ const startServer = async () => {
});
});
fastify.register(eventRouter, { prefix: '/api/event' });
fastify.register(eventRouter, { prefix: '/event' });
fastify.setErrorHandler((error, request, reply) => {
fastify.log.error(error);
});

2
apps/test/public/op.js Normal file
View File

@@ -0,0 +1,2 @@
"use strict";(()=>{function y(u,t,e){return function(s,o,v){let m=`${u}${s}`,l,h={"mixan-client-id":t,"Content-Type":"application/json"};return e&&(h["mixan-client-secret"]=e),new Promise(a=>{let f=n=>{clearTimeout(l),fetch(m,{headers:h,method:"POST",body:JSON.stringify(o??{}),keepalive:!0,...v??{}}).then(async p=>{if(p.status!==200)return g(n,a);let w=await p.json();if(!w)return a(null);a(w)}).catch(()=>g(n,a))};function g(n,p){if(n>1)return p(null);l=setTimeout(()=>{f(n+1)},Math.pow(2,n)*500)}f(0)})}}var c=class{options;api;state={properties:{}};constructor(t){this.options=t,this.api=y(t.url,t.clientId,t.clientSecret)}init(t){this.state.properties=t??{}}getProfileId(){if(this.state.profileId)return this.state.profileId;this.options.getProfileId&&(this.state.profileId=this.options.getProfileId()||void 0)}async event(t,e){let i=await this.api("/event",{name:t,properties:{...this.state.properties,...e??{}},timestamp:this.timestamp(),profileId:this.getProfileId()});this.options.setProfileId&&i&&this.options.setProfileId(i)}setGlobalProperties(t){this.state.properties={...this.state.properties,...t}}clear(){this.state.profileId=void 0,this.options.removeProfileId&&this.options.removeProfileId()}setUserProperty(t,e,i=!0){}timestamp(){return new Date().toISOString()}};var d=class extends c{constructor(t){super(t),this.options.trackOutgoingLinks&&this.trackOutgoingLinks(),this.options.trackScreenViews&&this.trackScreenViews()}isServer(){return typeof document>"u"}getTimezone(){try{return Intl.DateTimeFormat().resolvedOptions().timeZone}catch{return}}trackOutgoingLinks(){this.isServer()||document.addEventListener("click",t=>{let e=t.target;if(e.tagName==="A"){let i=e.getAttribute("href");i?.startsWith("http")&&super.event("link_out",{href:i,text:e.innerText})}})}trackScreenViews(){if(this.isServer())return;let t=history.pushState;history.pushState=function(...s){let o=t.apply(this,s);return window.dispatchEvent(new Event("pushstate")),window.dispatchEvent(new Event("locationchange")),o};let e=history.replaceState;history.replaceState=function(...s){let o=e.apply(this,s);return window.dispatchEvent(new Event("replacestate")),window.dispatchEvent(new Event("locationchange")),o},window.addEventListener("popstate",()=>window.dispatchEvent(new Event("locationchange"))),this.options.hash?window.addEventListener("hashchange",()=>this.screenView()):window.addEventListener("locationchange",()=>this.screenView())}screenView(t){this.isServer()||super.event("screen_view",{...t??{},path:window.location.href,title:document.title,referrer:document.referrer})}};var r=document.currentScript;r&&(window.openpanel=new d({url:r?.getAttribute("data-url"),clientId:r?.getAttribute("data-client-id"),clientSecret:r?.getAttribute("data-client-secret"),trackOutgoingLinks:!!r?.getAttribute("data-track-outgoing-links"),trackScreenViews:!!r?.getAttribute("data-track-screen-views")}));})();
//# sourceMappingURL=op.js.map

File diff suppressed because one or more lines are too long

View File

@@ -6,10 +6,11 @@ export default function Document() {
<Head>
<script
async
src="/cdn.global.js"
client-id="568b4ed1-5d00-4f27-88a7-b8959e6674bd"
client-secret="1e362905-d352-44c4-9263-e037a2ad52fb"
track-screen-views="true"
src="/op.js"
data-url="https://api.openpanel.dev"
data-client-id="301c6dc1-424c-4bc3-9886-a8beab09b615"
data-track-screen-views="1"
data-track-outgoing-links="1"
/>
</Head>
<body>

View File

@@ -5,10 +5,10 @@ import { MixanWeb as Openpanel } from './index';
const el = document.currentScript;
if (el) {
window.openpanel = new Openpanel({
url: el?.getAttribute('url'),
clientId: el?.getAttribute('client-id'),
clientSecret: el?.getAttribute('client-secret'),
trackOutgoingLinks: !!el?.getAttribute('track-outgoing-links'),
trackScreenViews: !!el?.getAttribute('track-screen-views'),
url: el?.getAttribute('data-url'),
clientId: el?.getAttribute('data-client-id'),
clientSecret: el?.getAttribute('data-client-secret'),
trackOutgoingLinks: !!el?.getAttribute('data-track-outgoing-links'),
trackScreenViews: !!el?.getAttribute('data-track-screen-views'),
});
}