@walkeros/web-source-session 3.1.0 → 3.1.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/dev.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,i=(e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})},r={};i(r,{examples:()=>d,schemas:()=>a}),module.exports=(e=r,((e,i,r,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of o(i))s.call(e,c)||c===r||n(e,c,{get:()=>i[c],enumerable:!(a=t(i,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var a={};i(a,{SettingsSchema:()=>l,settings:()=>g});var c=require("@walkeros/core/dev"),u=require("@walkeros/core/dev"),l=u.z.object({storage:u.z.boolean().default(!1).describe("Enable persistent storage for session/device IDs").optional(),consent:u.z.union([u.z.string(),u.z.array(u.z.string())]).describe("Consent key(s) required to enable storage mode").optional(),length:u.z.number().default(30).describe("Session timeout in minutes").optional(),pulse:u.z.boolean().default(!1).describe("Keep session alive on each event").optional(),sessionKey:u.z.string().default("elbSessionId").describe("Storage key for session ID").optional(),sessionStorage:u.z.enum(["local","session"]).default("local").describe("Storage type for session").optional(),deviceKey:u.z.string().default("elbDeviceId").describe("Storage key for device ID").optional(),deviceStorage:u.z.enum(["local","session"]).default("local").describe("Storage type for device").optional(),deviceAge:u.z.number().default(30).describe("Device ID age in days").optional(),cb:u.z.any().describe("Custom session callback function or false to disable").optional()}),g=(0,c.zodToSchema)(l),d={};i(d,{createTrigger:()=>W,step:()=>f,trigger:()=>X});var f={};i(f,{newMarketingSession:()=>p,returningVisitor:()=>b});var p={trigger:{type:"load",options:{url:"https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=winter-sale"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!0,count:1,runs:1,storage:!0,id:"s3ss10n-id",device:"d3v1c3-id",start:17e11,marketing:!0,source:"google",medium:"cpc",campaign:"winter-sale"},entity:"session",action:"start"}},b={trigger:{type:"load",options:{referrer:"https://google.com"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!1,count:3,runs:1,storage:!0,id:"n3w-s3ss10n",device:"d3v1c3-id",start:1700001e6,referrer:"google.com"},entity:"session",action:"start"}},y=require("@walkeros/core"),h=require("@walkeros/core"),v=require("@walkeros/core"),m=require("@walkeros/core"),w=require("@walkeros/core"),k=require("@walkeros/core"),O=require("@walkeros/core"),j=require("@walkeros/core"),q=require("@walkeros/core"),S=require("@walkeros/core"),D=require("@walkeros/core"),C={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"};function I(e){var n;const t={};for(const[o,s]of Object.entries(e))(null==(n=s.config)?void 0:n.next)?t[o]={next:s.config.next}:t[o]={};return t}function A(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function P(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function x(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:$(n.config.env)};s.debug("init");const r=await(0,O.useHooks)(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,env:(null==r?void 0:r.env)||n.config.env,init:!0},s.debug("init done")}return!0}async function E(e,n,t,o,s,i){const r=n.type||"unknown",a=e.logger.scope(`transformer:${r}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...$(n.config.env),...i?{respond:i}:{}}};a.debug("push",{event:o.name});const u=await(0,O.useHooks)(n.push,"TransformerPush",e.hooks)(o,c);return a.debug("push done"),u}async function z(e,n,t,o,s,i){let r=o,a=i;for(const o of t){const t=n[o];if(!t){e.logger.warn(`Transformer not found: ${o}`);continue}if(!await(0,O.tryCatchAsync)(x)(e,t,o))return e.logger.error(`Transformer init failed: ${o}`),null;const i=await(0,O.tryCatchAsync)(E,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s,a);if(!1===i)return null;if(i&&"object"==typeof i){const{event:t,respond:o,next:c}=i;if(o&&(a=o),c){const o=P(c,I(n));return o.length>0?z(e,n,o,t||r,s,a):(e.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(r=t)}}return r}function $(e){return e&&(0,O.isObject)(e)?e:{}}async function K(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c,u;const l=P(a,I(e.transformers)),g=e.logger.scope("source").scope(n),d={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,respond:u,mapping:s,preChain:l}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:g,...i},f={collector:e,logger:g,id:n,config:s,env:d,setIngest:async n=>{c=s.ingest?await(0,k.getMappingValue)(n,s.ingest,{collector:e}):void 0},setRespond:e=>{u=e}},p=await(0,k.tryCatchAsync)(o)(f);if(!p)return;const b=p.type||"unknown",y=e.logger.scope(b).scope(n);return d.logger=y,r&&(p.config={...p.config,primary:r}),p}function R(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:F(n.env,n.config.env)};(0,w.tryCatch)(n.on)(o,a)}async function H(e,n,t,o){let s,i=t||[];switch(t||(i=e.on[n]||[]),n){case C.Consent:s=o||e.consent;break;case C.Session:s=e.session;break;case C.User:s=o||e.user;break;case C.Custom:s=o||e.custom;break;case C.Globals:s=o||e.globals;break;case C.Config:s=o||e.config;break;case C.Ready:case C.Run:default:s=void 0}let r=!1;for(const t of Object.values(e.sources))t.on&&!1===await(0,w.tryCatchAsync)(t.on)(n,s)&&(r=!0);if(Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});R(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){var t,o;for(const[o,s]of Object.entries(e.pending.sources)){if(!e.pending.sources[o]||e.sources[o])continue;const i=null==(t=s.config)?void 0:t.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.sources[o];const a=await K(e,o,s);a&&(e.sources[o]=a)}for(const[t,s]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const i=null==(o=s.config)?void 0:o.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.destinations[t];const a=B(s);!1!==a.config.queue&&(a.queuePush=[...e.queue]),e.destinations[t]=a}}(e,n),!i.length)return!r;switch(n){case C.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,w.tryCatch)(n[t])(e,o)})})}(e,i,o);break;case C.Ready:case C.Run:c=i,(a=e).allowed&&c.forEach(e=>{(0,w.tryCatch)(e)(a)});break;case C.Session:!function(e,n){e.session&&n.forEach(n=>{(0,w.tryCatch)(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&(0,w.tryCatch)(n)(e,s)})}var a,c;return!r}async function M(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!(0,v.isFunction)(o.push))return G({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:F(o.env,i)};let l=u.config.id;if(!l)do{l=(0,v.getId)(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),N(e,void 0,{},{[l]:u})}async function N(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return G({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{var c,u;let l=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=(0,v.clone)(n);l.push(e)}if(!l.length&&!(null==(c=s.queueOn)?void 0:c.length))return{id:o,destination:s,skipped:!0};if(!l.length&&(null==(u=s.queueOn)?void 0:u.length)){const n=await(0,v.tryCatchAsync)(T)(e,s,o);return{id:o,destination:s,skipped:!n}}const g=[],d=l.filter(e=>{const n=(0,v.getGrantedConsent)(s.config.consent,i,e.consent);return!n||(e.consent=n,g.push(e),!1)});if(s.queuePush.push(...d),!g.length)return{id:o,destination:s,queue:l};if(!await(0,v.tryCatchAsync)(T)(e,s,o))return{id:o,destination:s,queue:l};let f,p;s.dlq||(s.dlq=[]);const b=function(e,n){const t=e.config.before;return t?P(t,I(n)):[]}(s,e.transformers);let y=0;return await Promise.all(g.map(async n=>{n.globals=(0,v.assign)(r,n.globals),n.user=(0,v.assign)(a,n.user);let i=n;if(b.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await z(e,e.transformers,b,n,t.ingest,t.respond);if(null===o)return n;i=o}const c=Date.now(),u=await(0,v.tryCatchAsync)(_,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),f=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest,t.respond);return y+=Date.now()-c,void 0!==u&&(p=u),n})),{id:o,destination:s,error:f,response:p,totalDuration:y}})),u={},l={},g={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,g[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return G({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(g).length&&{failed:g}})}async function T(e,n,t){var o;if(n.init&&!n.config.init){const s=n.type||"unknown",i=e.logger.scope(s),r={collector:e,logger:i,id:t,config:n.config,env:F(n.env,n.config.env)};i.debug("init");const a=await(0,v.useHooks)(n.init,"DestinationInit",e.hooks)(r);if(!1===a)return a;if(n.config={...a||n.config,init:!0},null==(o=n.queueOn)?void 0:o.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)R(e,n,t,s,i)}i.debug("init done")}return!0}async function _(e,n,t,o,s,i){const{config:r}=n,a=await(0,v.processEventMapping)(o,r,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:r,data:a.data,rule:a.mapping,ingest:s,env:{...F(n.env,r.env),...i?{respond:i}:{}}},g=a.mapping,d=a.mappingKey||"* *";if(!(null==g?void 0:g.batch)||!n.pushBatch){u.debug("push",{event:a.event.name});const t=await(0,v.useHooks)(n.push,"DestinationPush",e.hooks)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[d]){const o={key:d,events:[],data:[]};n.batches[d]={batched:o,batchFn:(0,v.debounce)(()=>{const o=n.batches[d].batched,a={collector:e,logger:u,id:t,config:r,data:void 0,rule:g,ingest:s,env:{...F(n.env,r.env),...i?{respond:i}:{}}};u.debug("push batch",{events:o.events.length}),(0,v.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},g.batch)}}const o=n.batches[d];o.batched.events.push(a.event),(0,v.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function G(e){return{ok:!(null==e?void 0:e.failed),...e}}function B(e){const{code:n,config:t={},env:o={}}=e,{config:s}=A(e,"before"),i={...n.config,...t,...s},r=F(n.env,o);return{...n,config:i,env:r}}function F(e,n){return e||n?n?e&&(0,v.isObject)(e)&&(0,v.isObject)(n)?{...e,...n}:n:e:{}}async function J(e,n,t){const o=Object.entries(e).map(async([e,o])=>{var s;const i=o.destroy;if(!i)return;const r=o.type||"unknown",a=t.scope(r),c={id:e,config:o.config,env:null!=(s=o.env)?s:{},logger:a};try{await Promise.race([i(c),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){a.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function U(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case C.Config:(0,q.isObject)(t)&&((0,j.assign)(e.config,t,{shallow:!1}),i=t,r=!0);break;case C.Consent:if((0,q.isObject)(t)){const{update:n,runQueue:o}=function(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=(0,y.assign)(e.consent,o),{update:o,runQueue:t}}(e,t);i=n,r=!0,a=o}break;case C.Custom:(0,q.isObject)(t)&&(e.custom=(0,j.assign)(e.custom,t),i=t,r=!0);break;case C.Destination:(0,q.isObject)(t)&&("code"in t&&(0,q.isObject)(t.code)?s=await M(e,t,o):(0,j.isFunction)(t.push)&&(s=await M(e,{code:t},o)));break;case C.Globals:(0,q.isObject)(t)&&(e.globals=(0,j.assign)(e.globals,t),i=t,r=!0);break;case C.On:(0,j.isString)(t)&&await async function(e,n,t){const o=e.on,s=o[n]||[],i=(0,m.isArray)(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await H(e,n,i)}(e,t,o);break;case C.Ready:r=!0;break;case C.Run:s=await async function(e,n){return e.allowed=!0,e.count=0,e.group=(0,j.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,j.assign)(e.consent,n.consent)),n.user&&(e.user=(0,j.assign)(e.user,n.user)),n.globals&&(e.globals=(0,j.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,j.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++,await N(e)}(e,t),r=!0;break;case C.Session:r=!0;break;case C.Shutdown:await async function(e){const n=e.logger;await J(e.sources,"source",n),await J(e.destinations,"destination",n),await J(e.transformers,"transformer",n),await J(e.stores,"store",n)}(e);break;case C.User:(0,q.isObject)(t)&&((0,j.assign)(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await H(e,n,void 0,i),a&&(s=await N(e)),s||G({ok:!0})}function V(e,n){return(0,S.useHooks)(async(t,o={})=>await(0,S.tryCatchAsync)(async()=>{const s=Date.now(),{id:i,ingest:r,respond:a,mapping:c,preChain:u}=o;let l=t;const g=r?Object.freeze(r):void 0;if(c){const n=await(0,S.processEventMapping)(l,c,e);if(n.ignore)return G({ok:!0});if(c.consent&&!(0,S.getGrantedConsent)(c.consent,e.consent,n.event.consent))return G({ok:!0});l=n.event}if((null==u?void 0:u.length)&&e.transformers&&Object.keys(e.transformers).length>0){const n=await z(e,e.transformers,u,l,g,a);if(null===n)return G({ok:!0});l=n}const d=n(l),f=function(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:d=e.user,nested:f=[],consent:p=e.consent,id:b=`${s}-${i}-${r}`,trigger:y="",entity:h=t,action:v=o,timing:m=0,version:w={source:e.version,tagging:e.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:d,nested:f,consent:p,id:b,trigger:y,entity:h,action:v,timestamp:s,timing:m,group:i,count:r,version:w,source:k}}(e,d),p=await N(e,f,{id:i,ingest:g,respond:a});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return p},()=>G({ok:!1}))(),"Push",e.hooks)}async function L(e){var n,t;const o=(0,h.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),s={level:null==(n=e.logger)?void 0:n.level,handler:null==(t=e.logger)?void 0:t.handler},i=(0,h.createLogger)(s),r={...o.globalsStatic,...e.globals},a={allowed:!1,config:o,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:r,group:"",hooks:{},logger:i,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.0.2",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};var c,u;a.push=V(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=(c=a,u=U,(0,D.useHooks)(async(e,n,t)=>await(0,D.tryCatchAsync)(async()=>await u(c,e,n,t),()=>G({ok:!1}))(),"Command",c.hooks));const l=e.stores||{};return a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:i={},env:r={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:i,env:r},u=await n(c);t[o]=u}return t}(a,l),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(l,a.stores,e),a.destinations=await async function(e,n={}){var t,o;const s={};for(const[i,r]of Object.entries(n))(null==(o=null==(t=r.config)?void 0:t.require)?void 0:o.length)?e.pending.destinations[i]=r:s[i]=B(r);return s}(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=A(s,"next"),a=Object.keys(i).length>0?{...r,env:i}:r,c=e.logger.scope("transformer").scope(o),u={collector:e,logger:c,id:o,config:a,env:i},l=await n(u);t[o]=l}return t}(a,e.transformers||{}),a}async function Q(e){e=e||{};const n=await L(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return G({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const i=await K(e,o,s);i&&(t[o]=i)}return t}(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),g=l.find(e=>e.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}var W=async e=>{let n;return{get flow(){return n},trigger:(t,o)=>async t=>{var s,i,r,a,c,u,l,g,d;const f=o||{};if(f.url){const e=new URL(f.url);window.history.replaceState({},"",e.pathname+e.search)}if(f.referrer&&Object.defineProperty(document,"referrer",{value:f.referrer,configurable:!0}),f.sessionData){const e=f.sessionKey||"elbSessionId";localStorage.setItem(e,JSON.stringify(f.sessionData))}if(f.deviceId){const e=f.deviceKey||"elbDeviceId";localStorage.setItem(e,f.deviceId)}if(!n){const t=await Q({...e,run:null==(s=e.run)||s});n={collector:t.collector,elb:t.elb};const o=f.sessionKey||(null==(c=null==(a=null==(r=null==(i=e.sources)?void 0:i.session)?void 0:r.config)?void 0:a.settings)?void 0:c.sessionKey)||"elbSessionId",p=f.deviceKey||(null==(d=null==(g=null==(l=null==(u=e.sources)?void 0:u.session)?void 0:l.config)?void 0:g.settings)?void 0:d.deviceKey)||"elbDeviceId";localStorage.removeItem(o),localStorage.removeItem(p),f.sessionData&&localStorage.setItem(o,JSON.stringify(f.sessionData)),f.deviceId&&localStorage.setItem(p,f.deviceId),e.consent&&await n.collector.command("consent",e.consent)}}}},X=(e,n)=>{if(!e||"object"!=typeof e)return;const t=e,o=n.localStorage;if(t.sessionData&&"object"==typeof t.sessionData){const e="string"==typeof t.sessionKey?t.sessionKey:"elbSessionId";o.setItem(e,JSON.stringify(t.sessionData))}if("string"==typeof t.deviceId){const e="string"==typeof t.deviceKey?t.deviceKey:"elbDeviceId";o.setItem(e,t.deviceId)}};//# sourceMappingURL=dev.js.map
1
+ "use strict";var e,n=Object.defineProperty,t=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,i=(e,t)=>{for(var o in t)n(e,o,{get:t[o],enumerable:!0})},r={};i(r,{examples:()=>d,schemas:()=>a}),module.exports=(e=r,((e,i,r,a)=>{if(i&&"object"==typeof i||"function"==typeof i)for(let c of o(i))s.call(e,c)||c===r||n(e,c,{get:()=>i[c],enumerable:!(a=t(i,c))||a.enumerable});return e})(n({},"__esModule",{value:!0}),e));var a={};i(a,{SettingsSchema:()=>l,settings:()=>g});var c=require("@walkeros/core/dev"),u=require("@walkeros/core/dev"),l=u.z.object({storage:u.z.boolean().default(!1).describe("Enable persistent storage for session/device IDs").optional(),consent:u.z.union([u.z.string(),u.z.array(u.z.string())]).describe("Consent key(s) required to enable storage mode").optional(),length:u.z.number().default(30).describe("Session timeout in minutes").optional(),pulse:u.z.boolean().default(!1).describe("Keep session alive on each event").optional(),sessionKey:u.z.string().default("elbSessionId").describe("Storage key for session ID").optional(),sessionStorage:u.z.enum(["local","session"]).default("local").describe("Storage type for session").optional(),deviceKey:u.z.string().default("elbDeviceId").describe("Storage key for device ID").optional(),deviceStorage:u.z.enum(["local","session"]).default("local").describe("Storage type for device").optional(),deviceAge:u.z.number().default(30).describe("Device ID age in days").optional(),cb:u.z.any().describe("Custom session callback function or false to disable").optional()}),g=(0,c.zodToSchema)(l),d={};i(d,{createTrigger:()=>W,step:()=>f,trigger:()=>X});var f={};i(f,{newMarketingSession:()=>p,returningVisitor:()=>b});var p={trigger:{type:"load",options:{url:"https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=winter-sale"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!0,count:1,runs:1,storage:!0,id:"s3ss10n-id",device:"d3v1c3-id",start:17e11,marketing:!0,source:"google",medium:"cpc",campaign:"winter-sale"},entity:"session",action:"start"}},b={trigger:{type:"load",options:{referrer:"https://google.com"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!1,count:3,runs:1,storage:!0,id:"n3w-s3ss10n",device:"d3v1c3-id",start:1700001e6,referrer:"google.com"},entity:"session",action:"start"}},y=require("@walkeros/core"),h=require("@walkeros/core"),v=require("@walkeros/core"),m=require("@walkeros/core"),w=require("@walkeros/core"),k=require("@walkeros/core"),O=require("@walkeros/core"),j=require("@walkeros/core"),q=require("@walkeros/core"),S=require("@walkeros/core"),D=require("@walkeros/core"),C={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"};function I(e){var n;const t={};for(const[o,s]of Object.entries(e))(null==(n=s.config)?void 0:n.next)?t[o]={next:s.config.next}:t[o]={};return t}function A(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function P(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function x(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:$(n.config.env)};s.debug("init");const r=await(0,O.useHooks)(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,env:(null==r?void 0:r.env)||n.config.env,init:!0},s.debug("init done")}return!0}async function E(e,n,t,o,s,i){const r=n.type||"unknown",a=e.logger.scope(`transformer:${r}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...$(n.config.env),...i?{respond:i}:{}}};a.debug("push",{event:o.name});const u=await(0,O.useHooks)(n.push,"TransformerPush",e.hooks)(o,c);return a.debug("push done"),u}async function z(e,n,t,o,s,i){let r=o,a=i;for(const o of t){const t=n[o];if(!t){e.logger.warn(`Transformer not found: ${o}`);continue}if(!await(0,O.tryCatchAsync)(x)(e,t,o))return e.logger.error(`Transformer init failed: ${o}`),null;const i=await(0,O.tryCatchAsync)(E,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s,a);if(!1===i)return null;if(i&&"object"==typeof i){const{event:t,respond:o,next:c}=i;if(o&&(a=o),c){const o=P(c,I(n));return o.length>0?z(e,n,o,t||r,s,a):(e.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(r=t)}}return r}function $(e){return e&&(0,O.isObject)(e)?e:{}}async function K(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c,u;const l=P(a,I(e.transformers)),g=e.logger.scope("source").scope(n),d={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,respond:u,mapping:s,preChain:l}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:g,...i},f={collector:e,logger:g,id:n,config:s,env:d,setIngest:async n=>{c=s.ingest?await(0,k.getMappingValue)(n,s.ingest,{collector:e}):void 0},setRespond:e=>{u=e}},p=await(0,k.tryCatchAsync)(o)(f);if(!p)return;const b=p.type||"unknown",y=e.logger.scope(b).scope(n);return d.logger=y,r&&(p.config={...p.config,primary:r}),p}function R(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:F(n.env,n.config.env)};(0,w.tryCatch)(n.on)(o,a)}async function H(e,n,t,o){let s,i=t||[];switch(t||(i=e.on[n]||[]),n){case C.Consent:s=o||e.consent;break;case C.Session:s=e.session;break;case C.User:s=o||e.user;break;case C.Custom:s=o||e.custom;break;case C.Globals:s=o||e.globals;break;case C.Config:s=o||e.config;break;case C.Ready:case C.Run:default:s=void 0}let r=!1;for(const t of Object.values(e.sources))t.on&&!1===await(0,w.tryCatchAsync)(t.on)(n,s)&&(r=!0);if(Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});R(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){var t,o;for(const[o,s]of Object.entries(e.pending.sources)){if(!e.pending.sources[o]||e.sources[o])continue;const i=null==(t=s.config)?void 0:t.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.sources[o];const a=await K(e,o,s);a&&(e.sources[o]=a)}for(const[t,s]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const i=null==(o=s.config)?void 0:o.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.destinations[t];const a=B(s);!1!==a.config.queue&&(a.queuePush=[...e.queue]),e.destinations[t]=a}}(e,n),!i.length)return!r;switch(n){case C.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{(0,w.tryCatch)(n[t])(e,o)})})}(e,i,o);break;case C.Ready:case C.Run:c=i,(a=e).allowed&&c.forEach(e=>{(0,w.tryCatch)(e)(a)});break;case C.Session:!function(e,n){e.session&&n.forEach(n=>{(0,w.tryCatch)(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&(0,w.tryCatch)(n)(e,s)})}var a,c;return!r}async function M(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!(0,v.isFunction)(o.push))return G({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:F(o.env,i)};let l=u.config.id;if(!l)do{l=(0,v.getId)(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),N(e,void 0,{},{[l]:u})}async function N(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return G({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{var c,u;let l=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=(0,v.clone)(n);l.push(e)}if(!l.length&&!(null==(c=s.queueOn)?void 0:c.length))return{id:o,destination:s,skipped:!0};if(!l.length&&(null==(u=s.queueOn)?void 0:u.length)){const n=await(0,v.tryCatchAsync)(T)(e,s,o);return{id:o,destination:s,skipped:!n}}const g=[],d=l.filter(e=>{const n=(0,v.getGrantedConsent)(s.config.consent,i,e.consent);return!n||(e.consent=n,g.push(e),!1)});if(s.queuePush.push(...d),!g.length)return{id:o,destination:s,queue:l};if(!await(0,v.tryCatchAsync)(T)(e,s,o))return{id:o,destination:s,queue:l};let f,p;s.dlq||(s.dlq=[]);const b=function(e,n){const t=e.config.before;return t?P(t,I(n)):[]}(s,e.transformers);let y=0;return await Promise.all(g.map(async n=>{n.globals=(0,v.assign)(r,n.globals),n.user=(0,v.assign)(a,n.user);let i=n;if(b.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await z(e,e.transformers,b,n,t.ingest,t.respond);if(null===o)return n;i=o}const c=Date.now(),u=await(0,v.tryCatchAsync)(_,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),f=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest,t.respond);return y+=Date.now()-c,void 0!==u&&(p=u),n})),{id:o,destination:s,error:f,response:p,totalDuration:y}})),u={},l={},g={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,g[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return G({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(g).length&&{failed:g}})}async function T(e,n,t){var o;if(n.init&&!n.config.init){const s=n.type||"unknown",i=e.logger.scope(s),r={collector:e,logger:i,id:t,config:n.config,env:F(n.env,n.config.env)};i.debug("init");const a=await(0,v.useHooks)(n.init,"DestinationInit",e.hooks)(r);if(!1===a)return a;if(n.config={...a||n.config,init:!0},null==(o=n.queueOn)?void 0:o.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)R(e,n,t,s,i)}i.debug("init done")}return!0}async function _(e,n,t,o,s,i){const{config:r}=n,a=await(0,v.processEventMapping)(o,r,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:r,data:a.data,rule:a.mapping,ingest:s,env:{...F(n.env,r.env),...i?{respond:i}:{}}},g=a.mapping,d=a.mappingKey||"* *";if(!(null==g?void 0:g.batch)||!n.pushBatch){u.debug("push",{event:a.event.name});const t=await(0,v.useHooks)(n.push,"DestinationPush",e.hooks)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[d]){const o={key:d,events:[],data:[]};n.batches[d]={batched:o,batchFn:(0,v.debounce)(()=>{const o=n.batches[d].batched,a={collector:e,logger:u,id:t,config:r,data:void 0,rule:g,ingest:s,env:{...F(n.env,r.env),...i?{respond:i}:{}}};u.debug("push batch",{events:o.events.length}),(0,v.useHooks)(n.pushBatch,"DestinationPushBatch",e.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},g.batch)}}const o=n.batches[d];o.batched.events.push(a.event),(0,v.isDefined)(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function G(e){return{ok:!(null==e?void 0:e.failed),...e}}function B(e){const{code:n,config:t={},env:o={}}=e,{config:s}=A(e,"before"),i={...n.config,...t,...s},r=F(n.env,o);return{...n,config:i,env:r}}function F(e,n){return e||n?n?e&&(0,v.isObject)(e)&&(0,v.isObject)(n)?{...e,...n}:n:e:{}}async function J(e,n,t){const o=Object.entries(e).map(async([e,o])=>{var s;const i=o.destroy;if(!i)return;const r=o.type||"unknown",a=t.scope(r),c={id:e,config:o.config,env:null!=(s=o.env)?s:{},logger:a};try{await Promise.race([i(c),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){a.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function U(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case C.Config:(0,q.isObject)(t)&&((0,j.assign)(e.config,t,{shallow:!1}),i=t,r=!0);break;case C.Consent:if((0,q.isObject)(t)){const{update:n,runQueue:o}=function(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=(0,y.assign)(e.consent,o),{update:o,runQueue:t}}(e,t);i=n,r=!0,a=o}break;case C.Custom:(0,q.isObject)(t)&&(e.custom=(0,j.assign)(e.custom,t),i=t,r=!0);break;case C.Destination:(0,q.isObject)(t)&&("code"in t&&(0,q.isObject)(t.code)?s=await M(e,t,o):(0,j.isFunction)(t.push)&&(s=await M(e,{code:t},o)));break;case C.Globals:(0,q.isObject)(t)&&(e.globals=(0,j.assign)(e.globals,t),i=t,r=!0);break;case C.On:(0,j.isString)(t)&&await async function(e,n,t){const o=e.on,s=o[n]||[],i=(0,m.isArray)(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await H(e,n,i)}(e,t,o);break;case C.Ready:r=!0;break;case C.Run:s=await async function(e,n){return e.allowed=!0,e.count=0,e.group=(0,j.getId)(),e.timing=Date.now(),n&&(n.consent&&(e.consent=(0,j.assign)(e.consent,n.consent)),n.user&&(e.user=(0,j.assign)(e.user,n.user)),n.globals&&(e.globals=(0,j.assign)(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=(0,j.assign)(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++,await N(e)}(e,t),r=!0;break;case C.Session:r=!0;break;case C.Shutdown:await async function(e){const n=e.logger;await J(e.sources,"source",n),await J(e.destinations,"destination",n),await J(e.transformers,"transformer",n),await J(e.stores,"store",n)}(e);break;case C.User:(0,q.isObject)(t)&&((0,j.assign)(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await H(e,n,void 0,i),a&&(s=await N(e)),s||G({ok:!0})}function V(e,n){return(0,S.useHooks)(async(t,o={})=>await(0,S.tryCatchAsync)(async()=>{const s=Date.now(),{id:i,ingest:r,respond:a,mapping:c,preChain:u}=o;let l=t;const g=r?Object.freeze(r):void 0;if(c){const n=await(0,S.processEventMapping)(l,c,e);if(n.ignore)return G({ok:!0});if(c.consent&&!(0,S.getGrantedConsent)(c.consent,e.consent,n.event.consent))return G({ok:!0});l=n.event}if((null==u?void 0:u.length)&&e.transformers&&Object.keys(e.transformers).length>0){const n=await z(e,e.transformers,u,l,g,a);if(null===n)return G({ok:!0});l=n}const d=n(l),f=function(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:g={},user:d=e.user,nested:f=[],consent:p=e.consent,id:b=`${s}-${i}-${r}`,trigger:y="",entity:h=t,action:v=o,timing:m=0,version:w={source:e.version,tagging:e.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:g,user:d,nested:f,consent:p,id:b,trigger:y,entity:h,action:v,timestamp:s,timing:m,group:i,count:r,version:w,source:k}}(e,d),p=await N(e,f,{id:i,ingest:g,respond:a});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return p},()=>G({ok:!1}))(),"Push",e.hooks)}async function L(e){var n,t;const o=(0,h.assign)({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),s={level:null==(n=e.logger)?void 0:n.level,handler:null==(t=e.logger)?void 0:t.handler},i=(0,h.createLogger)(s),r={...o.globalsStatic,...e.globals},a={allowed:!1,config:o,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:r,group:"",hooks:{},logger:i,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.1.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};var c,u;a.push=V(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=(c=a,u=U,(0,D.useHooks)(async(e,n,t)=>await(0,D.tryCatchAsync)(async()=>await u(c,e,n,t),()=>G({ok:!1}))(),"Command",c.hooks));const l=e.stores||{};return a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:i={},env:r={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:i,env:r},u=await n(c);t[o]=u}return t}(a,l),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(l,a.stores,e),a.destinations=await async function(e,n={}){var t,o;const s={};for(const[i,r]of Object.entries(n))(null==(o=null==(t=r.config)?void 0:t.require)?void 0:o.length)?e.pending.destinations[i]=r:s[i]=B(r);return s}(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=A(s,"next"),a=Object.keys(i).length>0?{...r,env:i}:r,c=e.logger.scope("transformer").scope(o),u={collector:e,logger:c,id:o,config:a,env:i},l=await n(u);t[o]=l}return t}(a,e.transformers||{}),a}async function Q(e){e=e||{};const n=await L(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return G({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const i=await K(e,o,s);i&&(t[o]=i)}return t}(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),g=l.find(e=>e.config.primary);return g?u=g.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}var W=async e=>{let n;return{get flow(){return n},trigger:(t,o)=>async t=>{var s,i,r,a,c,u,l,g,d;const f=o||{};if(f.url){const e=new URL(f.url);window.history.replaceState({},"",e.pathname+e.search)}if(f.referrer&&Object.defineProperty(document,"referrer",{value:f.referrer,configurable:!0}),f.sessionData){const e=f.sessionKey||"elbSessionId";localStorage.setItem(e,JSON.stringify(f.sessionData))}if(f.deviceId){const e=f.deviceKey||"elbDeviceId";localStorage.setItem(e,f.deviceId)}if(!n){const t=await Q({...e,run:null==(s=e.run)||s});n={collector:t.collector,elb:t.elb};const o=f.sessionKey||(null==(c=null==(a=null==(r=null==(i=e.sources)?void 0:i.session)?void 0:r.config)?void 0:a.settings)?void 0:c.sessionKey)||"elbSessionId",p=f.deviceKey||(null==(d=null==(g=null==(l=null==(u=e.sources)?void 0:u.session)?void 0:l.config)?void 0:g.settings)?void 0:d.deviceKey)||"elbDeviceId";localStorage.removeItem(o),localStorage.removeItem(p),f.sessionData&&localStorage.setItem(o,JSON.stringify(f.sessionData)),f.deviceId&&localStorage.setItem(p,f.deviceId),e.consent&&await n.collector.command("consent",e.consent)}}}},X=(e,n)=>{if(!e||"object"!=typeof e)return;const t=e,o=n.localStorage;if(t.sessionData&&"object"==typeof t.sessionData){const e="string"==typeof t.sessionKey?t.sessionKey:"elbSessionId";o.setItem(e,JSON.stringify(t.sessionData))}if("string"==typeof t.deviceId){const e="string"==typeof t.deviceKey?t.deviceKey:"elbDeviceId";o.setItem(e,t.deviceId)}};//# sourceMappingURL=dev.js.map
package/dist/dev.mjs CHANGED
@@ -1 +1 @@
1
- var e=Object.defineProperty,n=(n,t)=>{for(var o in t)e(n,o,{get:t[o],enumerable:!0})},t={};n(t,{SettingsSchema:()=>i,settings:()=>r});import{zodToSchema as o}from"@walkeros/core/dev";import{z as s}from"@walkeros/core/dev";var i=s.object({storage:s.boolean().default(!1).describe("Enable persistent storage for session/device IDs").optional(),consent:s.union([s.string(),s.array(s.string())]).describe("Consent key(s) required to enable storage mode").optional(),length:s.number().default(30).describe("Session timeout in minutes").optional(),pulse:s.boolean().default(!1).describe("Keep session alive on each event").optional(),sessionKey:s.string().default("elbSessionId").describe("Storage key for session ID").optional(),sessionStorage:s.enum(["local","session"]).default("local").describe("Storage type for session").optional(),deviceKey:s.string().default("elbDeviceId").describe("Storage key for device ID").optional(),deviceStorage:s.enum(["local","session"]).default("local").describe("Storage type for device").optional(),deviceAge:s.number().default(30).describe("Device ID age in days").optional(),cb:s.any().describe("Custom session callback function or false to disable").optional()}),r=o(i),a={};n(a,{createTrigger:()=>fe,step:()=>c,trigger:()=>pe});var c={};n(c,{newMarketingSession:()=>u,returningVisitor:()=>l});var u={trigger:{type:"load",options:{url:"https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=winter-sale"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!0,count:1,runs:1,storage:!0,id:"s3ss10n-id",device:"d3v1c3-id",start:17e11,marketing:!0,source:"google",medium:"cpc",campaign:"winter-sale"},entity:"session",action:"start"}},l={trigger:{type:"load",options:{referrer:"https://google.com"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!1,count:3,runs:1,storage:!0,id:"n3w-s3ss10n",device:"d3v1c3-id",start:1700001e6,referrer:"google.com"},entity:"session",action:"start"}};import{assign as d}from"@walkeros/core";import{assign as g,createLogger as f}from"@walkeros/core";import{assign as p,clone as m,debounce as b,getId as v,getGrantedConsent as h,isDefined as y,isFunction as w,isObject as k,processEventMapping as O,tryCatchAsync as j,useHooks as S}from"@walkeros/core";import{isArray as D}from"@walkeros/core";import{tryCatch as q,tryCatchAsync as I}from"@walkeros/core";import{getMappingValue as C,tryCatchAsync as A}from"@walkeros/core";import{isObject as P,tryCatchAsync as x,useHooks as E}from"@walkeros/core";import{assign as $,getId as K,isFunction as R,isString as M}from"@walkeros/core";import{isObject as N}from"@walkeros/core";import{getGrantedConsent as T,processEventMapping as G,tryCatchAsync as H,useHooks as _}from"@walkeros/core";import{useHooks as B,tryCatchAsync as F}from"@walkeros/core";var J={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"};function U(e){var n;const t={};for(const[o,s]of Object.entries(e))(null==(n=s.config)?void 0:n.next)?t[o]={next:s.config.next}:t[o]={};return t}function V(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function L(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function z(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:X(n.config.env)};s.debug("init");const r=await E(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,env:(null==r?void 0:r.env)||n.config.env,init:!0},s.debug("init done")}return!0}async function Q(e,n,t,o,s,i){const r=n.type||"unknown",a=e.logger.scope(`transformer:${r}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...X(n.config.env),...i?{respond:i}:{}}};a.debug("push",{event:o.name});const u=await E(n.push,"TransformerPush",e.hooks)(o,c);return a.debug("push done"),u}async function W(e,n,t,o,s,i){let r=o,a=i;for(const o of t){const t=n[o];if(!t){e.logger.warn(`Transformer not found: ${o}`);continue}if(!await x(z)(e,t,o))return e.logger.error(`Transformer init failed: ${o}`),null;const i=await x(Q,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s,a);if(!1===i)return null;if(i&&"object"==typeof i){const{event:t,respond:o,next:c}=i;if(o&&(a=o),c){const o=L(c,U(n));return o.length>0?W(e,n,o,t||r,s,a):(e.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(r=t)}}return r}function X(e){return e&&P(e)?e:{}}async function Y(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c,u;const l=L(a,U(e.transformers)),d=e.logger.scope("source").scope(n),g={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,respond:u,mapping:s,preChain:l}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:d,...i},f={collector:e,logger:d,id:n,config:s,env:g,setIngest:async n=>{c=s.ingest?await C(n,s.ingest,{collector:e}):void 0},setRespond:e=>{u=e}},p=await A(o)(f);if(!p)return;const m=p.type||"unknown",b=e.logger.scope(m).scope(n);return g.logger=b,r&&(p.config={...p.config,primary:r}),p}function Z(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:ae(n.env,n.config.env)};q(n.on)(o,a)}async function ee(e,n,t,o){let s,i=t||[];switch(t||(i=e.on[n]||[]),n){case J.Consent:s=o||e.consent;break;case J.Session:s=e.session;break;case J.User:s=o||e.user;break;case J.Custom:s=o||e.custom;break;case J.Globals:s=o||e.globals;break;case J.Config:s=o||e.config;break;case J.Ready:case J.Run:default:s=void 0}let r=!1;for(const t of Object.values(e.sources))t.on&&!1===await I(t.on)(n,s)&&(r=!0);if(Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});Z(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){var t,o;for(const[o,s]of Object.entries(e.pending.sources)){if(!e.pending.sources[o]||e.sources[o])continue;const i=null==(t=s.config)?void 0:t.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.sources[o];const a=await Y(e,o,s);a&&(e.sources[o]=a)}for(const[t,s]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const i=null==(o=s.config)?void 0:o.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.destinations[t];const a=re(s);!1!==a.config.queue&&(a.queuePush=[...e.queue]),e.destinations[t]=a}}(e,n),!i.length)return!r;switch(n){case J.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{q(n[t])(e,o)})})}(e,i,o);break;case J.Ready:case J.Run:c=i,(a=e).allowed&&c.forEach(e=>{q(e)(a)});break;case J.Session:!function(e,n){e.session&&n.forEach(n=>{q(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&q(n)(e,s)})}var a,c;return!r}async function ne(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!w(o.push))return ie({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:ae(o.env,i)};let l=u.config.id;if(!l)do{l=v(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),te(e,void 0,{},{[l]:u})}async function te(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return ie({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{var c,u;let l=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=m(n);l.push(e)}if(!l.length&&!(null==(c=s.queueOn)?void 0:c.length))return{id:o,destination:s,skipped:!0};if(!l.length&&(null==(u=s.queueOn)?void 0:u.length)){const n=await j(oe)(e,s,o);return{id:o,destination:s,skipped:!n}}const d=[],g=l.filter(e=>{const n=h(s.config.consent,i,e.consent);return!n||(e.consent=n,d.push(e),!1)});if(s.queuePush.push(...g),!d.length)return{id:o,destination:s,queue:l};if(!await j(oe)(e,s,o))return{id:o,destination:s,queue:l};let f,b;s.dlq||(s.dlq=[]);const v=function(e,n){const t=e.config.before;return t?L(t,U(n)):[]}(s,e.transformers);let y=0;return await Promise.all(d.map(async n=>{n.globals=p(r,n.globals),n.user=p(a,n.user);let i=n;if(v.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await W(e,e.transformers,v,n,t.ingest,t.respond);if(null===o)return n;i=o}const c=Date.now(),u=await j(se,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),f=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest,t.respond);return y+=Date.now()-c,void 0!==u&&(b=u),n})),{id:o,destination:s,error:f,response:b,totalDuration:y}})),u={},l={},d={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,d[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return ie({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(d).length&&{failed:d}})}async function oe(e,n,t){var o;if(n.init&&!n.config.init){const s=n.type||"unknown",i=e.logger.scope(s),r={collector:e,logger:i,id:t,config:n.config,env:ae(n.env,n.config.env)};i.debug("init");const a=await S(n.init,"DestinationInit",e.hooks)(r);if(!1===a)return a;if(n.config={...a||n.config,init:!0},null==(o=n.queueOn)?void 0:o.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)Z(e,n,t,s,i)}i.debug("init done")}return!0}async function se(e,n,t,o,s,i){const{config:r}=n,a=await O(o,r,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:r,data:a.data,rule:a.mapping,ingest:s,env:{...ae(n.env,r.env),...i?{respond:i}:{}}},d=a.mapping,g=a.mappingKey||"* *";if(!(null==d?void 0:d.batch)||!n.pushBatch){u.debug("push",{event:a.event.name});const t=await S(n.push,"DestinationPush",e.hooks)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[g]){const o={key:g,events:[],data:[]};n.batches[g]={batched:o,batchFn:b(()=>{const o=n.batches[g].batched,a={collector:e,logger:u,id:t,config:r,data:void 0,rule:d,ingest:s,env:{...ae(n.env,r.env),...i?{respond:i}:{}}};u.debug("push batch",{events:o.events.length}),S(n.pushBatch,"DestinationPushBatch",e.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},d.batch)}}const o=n.batches[g];o.batched.events.push(a.event),y(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function ie(e){return{ok:!(null==e?void 0:e.failed),...e}}function re(e){const{code:n,config:t={},env:o={}}=e,{config:s}=V(e,"before"),i={...n.config,...t,...s},r=ae(n.env,o);return{...n,config:i,env:r}}function ae(e,n){return e||n?n?e&&k(e)&&k(n)?{...e,...n}:n:e:{}}async function ce(e,n,t){const o=Object.entries(e).map(async([e,o])=>{var s;const i=o.destroy;if(!i)return;const r=o.type||"unknown",a=t.scope(r),c={id:e,config:o.config,env:null!=(s=o.env)?s:{},logger:a};try{await Promise.race([i(c),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){a.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function ue(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case J.Config:N(t)&&($(e.config,t,{shallow:!1}),i=t,r=!0);break;case J.Consent:if(N(t)){const{update:n,runQueue:o}=function(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=d(e.consent,o),{update:o,runQueue:t}}(e,t);i=n,r=!0,a=o}break;case J.Custom:N(t)&&(e.custom=$(e.custom,t),i=t,r=!0);break;case J.Destination:N(t)&&("code"in t&&N(t.code)?s=await ne(e,t,o):R(t.push)&&(s=await ne(e,{code:t},o)));break;case J.Globals:N(t)&&(e.globals=$(e.globals,t),i=t,r=!0);break;case J.On:M(t)&&await async function(e,n,t){const o=e.on,s=o[n]||[],i=D(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await ee(e,n,i)}(e,t,o);break;case J.Ready:r=!0;break;case J.Run:s=await async function(e,n){return e.allowed=!0,e.count=0,e.group=K(),e.timing=Date.now(),n&&(n.consent&&(e.consent=$(e.consent,n.consent)),n.user&&(e.user=$(e.user,n.user)),n.globals&&(e.globals=$(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=$(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++,await te(e)}(e,t),r=!0;break;case J.Session:r=!0;break;case J.Shutdown:await async function(e){const n=e.logger;await ce(e.sources,"source",n),await ce(e.destinations,"destination",n),await ce(e.transformers,"transformer",n),await ce(e.stores,"store",n)}(e);break;case J.User:N(t)&&($(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await ee(e,n,void 0,i),a&&(s=await te(e)),s||ie({ok:!0})}function le(e,n){return _(async(t,o={})=>await H(async()=>{const s=Date.now(),{id:i,ingest:r,respond:a,mapping:c,preChain:u}=o;let l=t;const d=r?Object.freeze(r):void 0;if(c){const n=await G(l,c,e);if(n.ignore)return ie({ok:!0});if(c.consent&&!T(c.consent,e.consent,n.event.consent))return ie({ok:!0});l=n.event}if((null==u?void 0:u.length)&&e.transformers&&Object.keys(e.transformers).length>0){const n=await W(e,e.transformers,u,l,d,a);if(null===n)return ie({ok:!0});l=n}const g=n(l),f=function(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:d={},user:g=e.user,nested:f=[],consent:p=e.consent,id:m=`${s}-${i}-${r}`,trigger:b="",entity:v=t,action:h=o,timing:y=0,version:w={source:e.version,tagging:e.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:d,user:g,nested:f,consent:p,id:m,trigger:b,entity:v,action:h,timestamp:s,timing:y,group:i,count:r,version:w,source:k}}(e,g),p=await te(e,f,{id:i,ingest:d,respond:a});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return p},()=>ie({ok:!1}))(),"Push",e.hooks)}async function de(e){var n,t;const o=g({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),s={level:null==(n=e.logger)?void 0:n.level,handler:null==(t=e.logger)?void 0:t.handler},i=f(s),r={...o.globalsStatic,...e.globals},a={allowed:!1,config:o,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:r,group:"",hooks:{},logger:i,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.0.2",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};var c,u;a.push=le(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=(u=ue,B(async(e,n,t)=>await F(async()=>await u(c,e,n,t),()=>ie({ok:!1}))(),"Command",(c=a).hooks));const l=e.stores||{};return a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:i={},env:r={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:i,env:r},u=await n(c);t[o]=u}return t}(a,l),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(l,a.stores,e),a.destinations=await async function(e,n={}){var t,o;const s={};for(const[i,r]of Object.entries(n))(null==(o=null==(t=r.config)?void 0:t.require)?void 0:o.length)?e.pending.destinations[i]=r:s[i]=re(r);return s}(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=V(s,"next"),a=Object.keys(i).length>0?{...r,env:i}:r,c=e.logger.scope("transformer").scope(o),u={collector:e,logger:c,id:o,config:a,env:i},l=await n(u);t[o]=l}return t}(a,e.transformers||{}),a}async function ge(e){e=e||{};const n=await de(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return ie({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const i=await Y(e,o,s);i&&(t[o]=i)}return t}(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),d=l.find(e=>e.config.primary);return d?u=d.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}var fe=async e=>{let n;return{get flow(){return n},trigger:(t,o)=>async t=>{var s,i,r,a,c,u,l,d,g;const f=o||{};if(f.url){const e=new URL(f.url);window.history.replaceState({},"",e.pathname+e.search)}if(f.referrer&&Object.defineProperty(document,"referrer",{value:f.referrer,configurable:!0}),f.sessionData){const e=f.sessionKey||"elbSessionId";localStorage.setItem(e,JSON.stringify(f.sessionData))}if(f.deviceId){const e=f.deviceKey||"elbDeviceId";localStorage.setItem(e,f.deviceId)}if(!n){const t=await ge({...e,run:null==(s=e.run)||s});n={collector:t.collector,elb:t.elb};const o=f.sessionKey||(null==(c=null==(a=null==(r=null==(i=e.sources)?void 0:i.session)?void 0:r.config)?void 0:a.settings)?void 0:c.sessionKey)||"elbSessionId",p=f.deviceKey||(null==(g=null==(d=null==(l=null==(u=e.sources)?void 0:u.session)?void 0:l.config)?void 0:d.settings)?void 0:g.deviceKey)||"elbDeviceId";localStorage.removeItem(o),localStorage.removeItem(p),f.sessionData&&localStorage.setItem(o,JSON.stringify(f.sessionData)),f.deviceId&&localStorage.setItem(p,f.deviceId),e.consent&&await n.collector.command("consent",e.consent)}}}},pe=(e,n)=>{if(!e||"object"!=typeof e)return;const t=e,o=n.localStorage;if(t.sessionData&&"object"==typeof t.sessionData){const e="string"==typeof t.sessionKey?t.sessionKey:"elbSessionId";o.setItem(e,JSON.stringify(t.sessionData))}if("string"==typeof t.deviceId){const e="string"==typeof t.deviceKey?t.deviceKey:"elbDeviceId";o.setItem(e,t.deviceId)}};export{a as examples,t as schemas};//# sourceMappingURL=dev.mjs.map
1
+ var e=Object.defineProperty,n=(n,t)=>{for(var o in t)e(n,o,{get:t[o],enumerable:!0})},t={};n(t,{SettingsSchema:()=>i,settings:()=>r});import{zodToSchema as o}from"@walkeros/core/dev";import{z as s}from"@walkeros/core/dev";var i=s.object({storage:s.boolean().default(!1).describe("Enable persistent storage for session/device IDs").optional(),consent:s.union([s.string(),s.array(s.string())]).describe("Consent key(s) required to enable storage mode").optional(),length:s.number().default(30).describe("Session timeout in minutes").optional(),pulse:s.boolean().default(!1).describe("Keep session alive on each event").optional(),sessionKey:s.string().default("elbSessionId").describe("Storage key for session ID").optional(),sessionStorage:s.enum(["local","session"]).default("local").describe("Storage type for session").optional(),deviceKey:s.string().default("elbDeviceId").describe("Storage key for device ID").optional(),deviceStorage:s.enum(["local","session"]).default("local").describe("Storage type for device").optional(),deviceAge:s.number().default(30).describe("Device ID age in days").optional(),cb:s.any().describe("Custom session callback function or false to disable").optional()}),r=o(i),a={};n(a,{createTrigger:()=>fe,step:()=>c,trigger:()=>pe});var c={};n(c,{newMarketingSession:()=>u,returningVisitor:()=>l});var u={trigger:{type:"load",options:{url:"https://example.com/?utm_source=google&utm_medium=cpc&utm_campaign=winter-sale"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!0,count:1,runs:1,storage:!0,id:"s3ss10n-id",device:"d3v1c3-id",start:17e11,marketing:!0,source:"google",medium:"cpc",campaign:"winter-sale"},entity:"session",action:"start"}},l={trigger:{type:"load",options:{referrer:"https://google.com"}},in:{storage:!0},out:{name:"session start",data:{isStart:!0,isNew:!1,count:3,runs:1,storage:!0,id:"n3w-s3ss10n",device:"d3v1c3-id",start:1700001e6,referrer:"google.com"},entity:"session",action:"start"}};import{assign as d}from"@walkeros/core";import{assign as g,createLogger as f}from"@walkeros/core";import{assign as p,clone as m,debounce as b,getId as v,getGrantedConsent as h,isDefined as y,isFunction as w,isObject as k,processEventMapping as O,tryCatchAsync as j,useHooks as S}from"@walkeros/core";import{isArray as D}from"@walkeros/core";import{tryCatch as q,tryCatchAsync as I}from"@walkeros/core";import{getMappingValue as C,tryCatchAsync as A}from"@walkeros/core";import{isObject as P,tryCatchAsync as x,useHooks as E}from"@walkeros/core";import{assign as $,getId as K,isFunction as R,isString as M}from"@walkeros/core";import{isObject as N}from"@walkeros/core";import{getGrantedConsent as T,processEventMapping as G,tryCatchAsync as H,useHooks as _}from"@walkeros/core";import{useHooks as B,tryCatchAsync as F}from"@walkeros/core";var J={Action:"action",Actions:"actions",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",Shutdown:"shutdown",User:"user",Walker:"walker"};function U(e){var n;const t={};for(const[o,s]of Object.entries(e))(null==(n=s.config)?void 0:n.next)?t[o]={next:s.config.next}:t[o]={};return t}function V(e,n){const t=e.config||{},o=e[n];return void 0!==o?{config:{...t,[n]:o},chainValue:o}:{config:t,chainValue:void 0}}function L(e,n={}){if(!e)return[];if(Array.isArray(e))return e;const t=[],o=new Set;let s=e;for(;s&&n[s]&&!o.has(s);){o.add(s),t.push(s);const e=n[s].next;if(Array.isArray(e)){t.push(...e);break}s=e}return t}async function z(e,n,t){if(n.init&&!n.config.init){const o=n.type||"unknown",s=e.logger.scope(`transformer:${o}`),i={collector:e,logger:s,id:t,config:n.config,env:X(n.config.env)};s.debug("init");const r=await E(n.init,"TransformerInit",e.hooks)(i);if(!1===r)return!1;n.config={...r||n.config,env:(null==r?void 0:r.env)||n.config.env,init:!0},s.debug("init done")}return!0}async function Q(e,n,t,o,s,i){const r=n.type||"unknown",a=e.logger.scope(`transformer:${r}`),c={collector:e,logger:a,id:t,ingest:s,config:n.config,env:{...X(n.config.env),...i?{respond:i}:{}}};a.debug("push",{event:o.name});const u=await E(n.push,"TransformerPush",e.hooks)(o,c);return a.debug("push done"),u}async function W(e,n,t,o,s,i){let r=o,a=i;for(const o of t){const t=n[o];if(!t){e.logger.warn(`Transformer not found: ${o}`);continue}if(!await x(z)(e,t,o))return e.logger.error(`Transformer init failed: ${o}`),null;const i=await x(Q,n=>(e.logger.scope(`transformer:${t.type||"unknown"}`).error("Push failed",{error:n}),!1))(e,t,o,r,s,a);if(!1===i)return null;if(i&&"object"==typeof i){const{event:t,respond:o,next:c}=i;if(o&&(a=o),c){const o=L(c,U(n));return o.length>0?W(e,n,o,t||r,s,a):(e.logger.warn(`Branch target not found: ${JSON.stringify(c)}`),null)}t&&(r=t)}}return r}function X(e){return e&&P(e)?e:{}}async function Y(e,n,t){const{code:o,config:s={},env:i={},primary:r,next:a}=t;let c,u;const l=L(a,U(e.transformers)),d=e.logger.scope("source").scope(n),g={push:(t,o={})=>e.push(t,{...o,id:n,ingest:c,respond:u,mapping:s,preChain:l}),command:e.command,sources:e.sources,elb:e.sources.elb.push,logger:d,...i},f={collector:e,logger:d,id:n,config:s,env:g,setIngest:async n=>{c=s.ingest?await C(n,s.ingest,{collector:e}):void 0},setRespond:e=>{u=e}},p=await A(o)(f);if(!p)return;const m=p.type||"unknown",b=e.logger.scope(m).scope(n);return g.logger=b,r&&(p.config={...p.config,primary:r}),p}function Z(e,n,t,o,s){if(!n.on)return;const i=n.type||"unknown",r=e.logger.scope(i).scope("on").scope(o),a={collector:e,logger:r,id:t,config:n.config,data:s,env:ae(n.env,n.config.env)};q(n.on)(o,a)}async function ee(e,n,t,o){let s,i=t||[];switch(t||(i=e.on[n]||[]),n){case J.Consent:s=o||e.consent;break;case J.Session:s=e.session;break;case J.User:s=o||e.user;break;case J.Custom:s=o||e.custom;break;case J.Globals:s=o||e.globals;break;case J.Config:s=o||e.config;break;case J.Ready:case J.Run:default:s=void 0}let r=!1;for(const t of Object.values(e.sources))t.on&&!1===await I(t.on)(n,s)&&(r=!0);if(Object.entries(e.destinations).forEach(([t,o])=>{if(o.on){if(!o.config.init)return o.queueOn=o.queueOn||[],void o.queueOn.push({type:n,data:s});Z(e,o,t,n,s)}}),(Object.keys(e.pending.sources).length>0||Object.keys(e.pending.destinations).length>0)&&await async function(e,n){var t,o;for(const[o,s]of Object.entries(e.pending.sources)){if(!e.pending.sources[o]||e.sources[o])continue;const i=null==(t=s.config)?void 0:t.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.sources[o];const a=await Y(e,o,s);a&&(e.sources[o]=a)}for(const[t,s]of Object.entries(e.pending.destinations)){if(!e.pending.destinations[t]||e.destinations[t])continue;const i=null==(o=s.config)?void 0:o.require;if(!i)continue;const r=i.indexOf(n);if(-1===r)continue;if(i.splice(r,1),i.length>0)continue;delete e.pending.destinations[t];const a=re(s);!1!==a.config.queue&&(a.queuePush=[...e.queue]),e.destinations[t]=a}}(e,n),!i.length)return!r;switch(n){case J.Consent:!function(e,n,t){const o=t||e.consent;n.forEach(n=>{Object.keys(o).filter(e=>e in n).forEach(t=>{q(n[t])(e,o)})})}(e,i,o);break;case J.Ready:case J.Run:c=i,(a=e).allowed&&c.forEach(e=>{q(e)(a)});break;case J.Session:!function(e,n){e.session&&n.forEach(n=>{q(n)(e,e.session)})}(e,i);break;default:i.forEach(n=>{"function"==typeof n&&q(n)(e,s)})}var a,c;return!r}async function ne(e,n,t){const{code:o,config:s={},env:i={},before:r}=n;if(!w(o.push))return ie({ok:!1,failed:{invalid:{type:"invalid",error:"Destination code must have a push method"}}});const a=t||s||{init:!1},c=r?{...a,before:r}:a,u={...o,config:c,env:ae(o.env,i)};let l=u.config.id;if(!l)do{l=v(4)}while(e.destinations[l]);return e.destinations[l]=u,!1!==u.config.queue&&(u.queuePush=[...e.queue]),te(e,void 0,{},{[l]:u})}async function te(e,n,t={},o){const{allowed:s,consent:i,globals:r,user:a}=e;if(!s)return ie({ok:!1});n&&(e.queue.push(n),e.status.in++),o||(o=e.destinations);const c=await Promise.all(Object.entries(o||{}).map(async([o,s])=>{var c,u;let l=(s.queuePush||[]).map(e=>({...e,consent:i}));if(s.queuePush=[],n){const e=m(n);l.push(e)}if(!l.length&&!(null==(c=s.queueOn)?void 0:c.length))return{id:o,destination:s,skipped:!0};if(!l.length&&(null==(u=s.queueOn)?void 0:u.length)){const n=await j(oe)(e,s,o);return{id:o,destination:s,skipped:!n}}const d=[],g=l.filter(e=>{const n=h(s.config.consent,i,e.consent);return!n||(e.consent=n,d.push(e),!1)});if(s.queuePush.push(...g),!d.length)return{id:o,destination:s,queue:l};if(!await j(oe)(e,s,o))return{id:o,destination:s,queue:l};let f,b;s.dlq||(s.dlq=[]);const v=function(e,n){const t=e.config.before;return t?L(t,U(n)):[]}(s,e.transformers);let y=0;return await Promise.all(d.map(async n=>{n.globals=p(r,n.globals),n.user=p(a,n.user);let i=n;if(v.length>0&&e.transformers&&Object.keys(e.transformers).length>0){const o=await W(e,e.transformers,v,n,t.ingest,t.respond);if(null===o)return n;i=o}const c=Date.now(),u=await j(se,n=>{const t=s.type||"unknown";e.logger.scope(t).error("Push failed",{error:n,event:i.name}),f=n,s.dlq.push([i,n])})(e,s,o,i,t.ingest,t.respond);return y+=Date.now()-c,void 0!==u&&(b=u),n})),{id:o,destination:s,error:f,response:b,totalDuration:y}})),u={},l={},d={};for(const n of c){if(n.skipped)continue;const t={type:n.destination.type||"unknown",data:n.response};e.status.destinations[n.id]||(e.status.destinations[n.id]={count:0,failed:0,duration:0});const o=e.status.destinations[n.id],s=Date.now();n.error?(t.error=n.error,d[n.id]=t,o.failed++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.failed++):n.queue&&n.queue.length?l[n.id]=t:(u[n.id]=t,o.count++,o.lastAt=s,o.duration+=n.totalDuration||0,e.status.out++)}return ie({event:n,...Object.keys(u).length&&{done:u},...Object.keys(l).length&&{queued:l},...Object.keys(d).length&&{failed:d}})}async function oe(e,n,t){var o;if(n.init&&!n.config.init){const s=n.type||"unknown",i=e.logger.scope(s),r={collector:e,logger:i,id:t,config:n.config,env:ae(n.env,n.config.env)};i.debug("init");const a=await S(n.init,"DestinationInit",e.hooks)(r);if(!1===a)return a;if(n.config={...a||n.config,init:!0},null==(o=n.queueOn)?void 0:o.length){const o=n.queueOn;n.queueOn=[];for(const{type:s,data:i}of o)Z(e,n,t,s,i)}i.debug("init done")}return!0}async function se(e,n,t,o,s,i){const{config:r}=n,a=await O(o,r,e);if(a.ignore)return!1;const c=n.type||"unknown",u=e.logger.scope(c),l={collector:e,logger:u,id:t,config:r,data:a.data,rule:a.mapping,ingest:s,env:{...ae(n.env,r.env),...i?{respond:i}:{}}},d=a.mapping,g=a.mappingKey||"* *";if(!(null==d?void 0:d.batch)||!n.pushBatch){u.debug("push",{event:a.event.name});const t=await S(n.push,"DestinationPush",e.hooks)(a.event,l);return u.debug("push done"),t}{if(n.batches=n.batches||{},!n.batches[g]){const o={key:g,events:[],data:[]};n.batches[g]={batched:o,batchFn:b(()=>{const o=n.batches[g].batched,a={collector:e,logger:u,id:t,config:r,data:void 0,rule:d,ingest:s,env:{...ae(n.env,r.env),...i?{respond:i}:{}}};u.debug("push batch",{events:o.events.length}),S(n.pushBatch,"DestinationPushBatch",e.hooks)(o,a),u.debug("push batch done"),o.events=[],o.data=[]},d.batch)}}const o=n.batches[g];o.batched.events.push(a.event),y(a.data)&&o.batched.data.push(a.data),o.batchFn()}return!0}function ie(e){return{ok:!(null==e?void 0:e.failed),...e}}function re(e){const{code:n,config:t={},env:o={}}=e,{config:s}=V(e,"before"),i={...n.config,...t,...s},r=ae(n.env,o);return{...n,config:i,env:r}}function ae(e,n){return e||n?n?e&&k(e)&&k(n)?{...e,...n}:n:e:{}}async function ce(e,n,t){const o=Object.entries(e).map(async([e,o])=>{var s;const i=o.destroy;if(!i)return;const r=o.type||"unknown",a=t.scope(r),c={id:e,config:o.config,env:null!=(s=o.env)?s:{},logger:a};try{await Promise.race([i(c),new Promise((t,o)=>setTimeout(()=>o(new Error(`${n} '${e}' destroy timed out`)),5e3))])}catch(t){a.error(`${n} '${e}' destroy failed: ${t}`)}});await Promise.allSettled(o)}async function ue(e,n,t,o){let s,i,r=!1,a=!1;switch(n){case J.Config:N(t)&&($(e.config,t,{shallow:!1}),i=t,r=!0);break;case J.Consent:if(N(t)){const{update:n,runQueue:o}=function(e,n){let t=!1;const o={};return Object.entries(n).forEach(([e,n])=>{const s=!!n;o[e]=s,t=t||s}),e.consent=d(e.consent,o),{update:o,runQueue:t}}(e,t);i=n,r=!0,a=o}break;case J.Custom:N(t)&&(e.custom=$(e.custom,t),i=t,r=!0);break;case J.Destination:N(t)&&("code"in t&&N(t.code)?s=await ne(e,t,o):R(t.push)&&(s=await ne(e,{code:t},o)));break;case J.Globals:N(t)&&(e.globals=$(e.globals,t),i=t,r=!0);break;case J.On:M(t)&&await async function(e,n,t){const o=e.on,s=o[n]||[],i=D(t)?t:[t];i.forEach(e=>{s.push(e)}),o[n]=s,await ee(e,n,i)}(e,t,o);break;case J.Ready:r=!0;break;case J.Run:s=await async function(e,n){return e.allowed=!0,e.count=0,e.group=K(),e.timing=Date.now(),n&&(n.consent&&(e.consent=$(e.consent,n.consent)),n.user&&(e.user=$(e.user,n.user)),n.globals&&(e.globals=$(e.config.globalsStatic||{},n.globals)),n.custom&&(e.custom=$(e.custom,n.custom))),Object.values(e.destinations).forEach(e=>{e.queuePush=[]}),e.queue=[],e.round++,await te(e)}(e,t),r=!0;break;case J.Session:r=!0;break;case J.Shutdown:await async function(e){const n=e.logger;await ce(e.sources,"source",n),await ce(e.destinations,"destination",n),await ce(e.transformers,"transformer",n),await ce(e.stores,"store",n)}(e);break;case J.User:N(t)&&($(e.user,t,{shallow:!1}),i=t,r=!0)}return r&&await ee(e,n,void 0,i),a&&(s=await te(e)),s||ie({ok:!0})}function le(e,n){return _(async(t,o={})=>await H(async()=>{const s=Date.now(),{id:i,ingest:r,respond:a,mapping:c,preChain:u}=o;let l=t;const d=r?Object.freeze(r):void 0;if(c){const n=await G(l,c,e);if(n.ignore)return ie({ok:!0});if(c.consent&&!T(c.consent,e.consent,n.event.consent))return ie({ok:!0});l=n.event}if((null==u?void 0:u.length)&&e.transformers&&Object.keys(e.transformers).length>0){const n=await W(e,e.transformers,u,l,d,a);if(null===n)return ie({ok:!0});l=n}const g=n(l),f=function(e,n){if(!n.name)throw new Error("Event name is required");const[t,o]=n.name.split(" ");if(!t||!o)throw new Error("Event name is invalid");++e.count;const{timestamp:s=Date.now(),group:i=e.group,count:r=e.count}=n,{name:a=`${t} ${o}`,data:c={},context:u={},globals:l=e.globals,custom:d={},user:g=e.user,nested:f=[],consent:p=e.consent,id:m=`${s}-${i}-${r}`,trigger:b="",entity:v=t,action:h=o,timing:y=0,version:w={source:e.version,tagging:e.config.tagging||0},source:k={type:"collector",id:"",previous_id:""}}=n;return{name:a,data:c,context:u,globals:l,custom:d,user:g,nested:f,consent:p,id:m,trigger:b,entity:v,action:h,timestamp:s,timing:y,group:i,count:r,version:w,source:k}}(e,g),p=await te(e,f,{id:i,ingest:d,respond:a});if(i){e.status.sources[i]||(e.status.sources[i]={count:0,duration:0});const n=e.status.sources[i];n.count++,n.lastAt=Date.now(),n.duration+=Date.now()-s}return p},()=>ie({ok:!1}))(),"Push",e.hooks)}async function de(e){var n,t;const o=g({globalsStatic:{},sessionStatic:{},tagging:0,run:!0},e,{merge:!1,extend:!1}),s={level:null==(n=e.logger)?void 0:n.level,handler:null==(t=e.logger)?void 0:t.handler},i=f(s),r={...o.globalsStatic,...e.globals},a={allowed:!1,config:o,consent:e.consent||{},count:0,custom:e.custom||{},destinations:{},transformers:{},stores:{},globals:r,group:"",hooks:{},logger:i,on:{},queue:[],round:0,session:void 0,status:{startedAt:Date.now(),in:0,out:0,failed:0,sources:{},destinations:{}},timing:Date.now(),user:e.user||{},version:"3.1.0",sources:{},pending:{sources:{},destinations:{}},push:void 0,command:void 0};var c,u;a.push=le(a,e=>({timing:Math.round((Date.now()-a.timing)/10)/100,source:{type:"collector",id:"",previous_id:""},...e})),a.command=(u=ue,B(async(e,n,t)=>await F(async()=>await u(c,e,n,t),()=>ie({ok:!1}))(),"Command",(c=a).hooks));const l=e.stores||{};return a.stores=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,config:i={},env:r={}}=s,a=e.logger.scope("store").scope(o),c={collector:e,logger:a,id:o,config:i,env:r},u=await n(c);t[o]=u}return t}(a,l),function(e,n,t){const o=new Map;for(const[t,s]of Object.entries(e))n[t]&&o.set(s,n[t]);if(0!==o.size)for(const e of[t.transformers,t.destinations,t.sources])if(e)for(const n of Object.values(e))s(n.env);function s(e){if(e)for(const[n,t]of Object.entries(e))if("object"==typeof t&&null!==t){const s=o.get(t);s&&(e[n]=s)}}}(l,a.stores,e),a.destinations=await async function(e,n={}){var t,o;const s={};for(const[i,r]of Object.entries(n))(null==(o=null==(t=r.config)?void 0:t.require)?void 0:o.length)?e.pending.destinations[i]=r:s[i]=re(r);return s}(a,e.destinations||{}),a.transformers=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{code:n,env:i={}}=s,{config:r}=V(s,"next"),a=Object.keys(i).length>0?{...r,env:i}:r,c=e.logger.scope("transformer").scope(o),u={collector:e,logger:c,id:o,config:a,env:i},l=await n(u);t[o]=l}return t}(a,e.transformers||{}),a}async function ge(e){e=e||{};const n=await de(e),t=(o=n,{type:"elb",config:{},push:async(e,n,t,s,i,r)=>{if("string"==typeof e&&e.startsWith("walker ")){const s=e.replace("walker ","");return o.command(s,n,t)}let a;if("string"==typeof e)a={name:e},n&&"object"==typeof n&&!Array.isArray(n)&&(a.data=n);else{if(!e||"object"!=typeof e)return ie({ok:!1});a=e,n&&"object"==typeof n&&!Array.isArray(n)&&(a.data={...a.data||{},...n})}return s&&"object"==typeof s&&(a.context=s),i&&Array.isArray(i)&&(a.nested=i),r&&"object"==typeof r&&(a.custom=r),o.push(a)}});var o;n.sources.elb=t;const s=await async function(e,n={}){const t={};for(const[o,s]of Object.entries(n)){const{config:n={}}=s;if(n.require&&n.require.length>0){e.pending.sources[o]=s;continue}const i=await Y(e,o,s);i&&(t[o]=i)}return t}(n,e.sources||{});Object.assign(n.sources,s);const{consent:i,user:r,globals:a,custom:c}=e;i&&await n.command("consent",i),r&&await n.command("user",r),a&&Object.assign(n.globals,a),c&&Object.assign(n.custom,c),n.config.run&&await n.command("run");let u=t.push;const l=Object.values(n.sources).filter(e=>"elb"!==e.type),d=l.find(e=>e.config.primary);return d?u=d.push:l.length>0&&(u=l[0].push),{collector:n,elb:u}}var fe=async e=>{let n;return{get flow(){return n},trigger:(t,o)=>async t=>{var s,i,r,a,c,u,l,d,g;const f=o||{};if(f.url){const e=new URL(f.url);window.history.replaceState({},"",e.pathname+e.search)}if(f.referrer&&Object.defineProperty(document,"referrer",{value:f.referrer,configurable:!0}),f.sessionData){const e=f.sessionKey||"elbSessionId";localStorage.setItem(e,JSON.stringify(f.sessionData))}if(f.deviceId){const e=f.deviceKey||"elbDeviceId";localStorage.setItem(e,f.deviceId)}if(!n){const t=await ge({...e,run:null==(s=e.run)||s});n={collector:t.collector,elb:t.elb};const o=f.sessionKey||(null==(c=null==(a=null==(r=null==(i=e.sources)?void 0:i.session)?void 0:r.config)?void 0:a.settings)?void 0:c.sessionKey)||"elbSessionId",p=f.deviceKey||(null==(g=null==(d=null==(l=null==(u=e.sources)?void 0:u.session)?void 0:l.config)?void 0:d.settings)?void 0:g.deviceKey)||"elbDeviceId";localStorage.removeItem(o),localStorage.removeItem(p),f.sessionData&&localStorage.setItem(o,JSON.stringify(f.sessionData)),f.deviceId&&localStorage.setItem(p,f.deviceId),e.consent&&await n.collector.command("consent",e.consent)}}}},pe=(e,n)=>{if(!e||"object"!=typeof e)return;const t=e,o=n.localStorage;if(t.sessionData&&"object"==typeof t.sessionData){const e="string"==typeof t.sessionKey?t.sessionKey:"elbSessionId";o.setItem(e,JSON.stringify(t.sessionData))}if("string"==typeof t.deviceId){const e="string"==typeof t.deviceKey?t.deviceKey:"elbDeviceId";o.setItem(e,t.deviceId)}};export{a as examples,t as schemas};//# sourceMappingURL=dev.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$meta": {
3
3
  "package": "@walkeros/web-source-session",
4
- "version": "3.0.2",
4
+ "version": "3.1.0",
5
5
  "type": "source",
6
6
  "platform": [
7
7
  "web"
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@walkeros/web-source-session",
3
3
  "description": "Session source for walkerOS",
4
- "version": "3.1.0",
4
+ "version": "3.1.1",
5
5
  "license": "MIT",
6
6
  "main": "./dist/index.js",
7
7
  "module": "./dist/index.mjs",
@@ -31,11 +31,11 @@
31
31
  "update": "npx npm-check-updates -u && npm update"
32
32
  },
33
33
  "dependencies": {
34
- "@walkeros/core": "^3.1.0",
35
- "@walkeros/web-core": "^3.1.0"
34
+ "@walkeros/core": "^3.1.1",
35
+ "@walkeros/web-core": "^3.1.1"
36
36
  },
37
37
  "devDependencies": {
38
- "@walkeros/collector": "^3.1.0"
38
+ "@walkeros/collector": "^3.1.1"
39
39
  },
40
40
  "repository": {
41
41
  "url": "git+https://github.com/elbwalker/walkerOS.git",