UNPKG

promoted-snowplow-logger

Version:

Logging utility for browser-side Typescript that logs Promoted events to our Metrics Snowplow-compatible API

3 lines (2 loc) 2.37 kB
class e{constructor(){this.logCohortMembership=()=>{},this.logView=()=>{},this.logImpression=()=>{},this.logAction=()=>{},this.logClick=()=>{},this.flushEarlyEvents=()=>{}}}const s=e=>e?[{schema:"iglu:ai.promoted/pageview_cx/jsonschema/2-0-0",data:e}]:[],o=s=>void 0===s.enabled||s.enabled?new r(s):new e,t=e=>new Error(`${e} is required`);class r{constructor(e){this.validateArgs=()=>{if(!this.handleError)throw t("handleError");if(!this.snowplow)throw t("snowplow");if(!this.snowplow.trackSelfDescribingEvent)throw t("trackSelfDescribingEvent");if(!this.snowplow.trackPageView)throw t("trackPageView")},this.logCohortMembership=e=>{this.snowplow.trackSelfDescribingEvent({event:{schema:"iglu:ai.promoted/cohortmembership/jsonschema/1-0-0",data:this.mergeBaseUserInfo(e)}},this.snowplowTrackers)},this.logView=e=>{this.snowplow.trackPageView({context:s(this.mergeBaseUserInfo(e))},this.snowplowTrackers)},this.logImpression=e=>{this.snowplow.trackSelfDescribingEvent({event:{schema:"iglu:ai.promoted/impression/jsonschema/1-0-0",data:this.mergeBaseUserInfo(e)}},this.snowplowTrackers)},this.logAction=e=>{this.prepareAction(e),this.snowplow.trackSelfDescribingEvent({event:{schema:"iglu:ai.promoted/action/jsonschema/1-0-0",data:this.mergeBaseUserInfo(e)}},this.snowplowTrackers)},this.prepareAction=e=>{const{cart:s}=e;if(s)for(const e of s.contents){0==e.quantity&&this.handleError(new Error("CartContent.quantity should be non-zero"));const{pricePerUnit:s}=e;s&&void 0!==s.amount&&(void 0!==s.amountMicros&&this.handleError(new Error("Do not set both amount and amountMicros")),e.pricePerUnit={currencyCode:s.currencyCode,amountMicros:Math.round(1e6*s.amount)})}},this.logClick=e=>{const s=Object.assign({actionType:2},e),{targetUrl:o}=e;delete s.targetUrl,o&&(s.navigateAction={targetUrl:o}),this.logAction(s)},this.mergeBaseUserInfo=e=>{var s;const o=null===(s=this.getUserInfo)||void 0===s?void 0:s.call(this);if(o){const{userInfo:s}=e;e.userInfo=Object.assign(Object.assign({},o),s)}return e},this.arrayWrap=e=>Array.isArray(e)?e:e?[e]:void 0,this.handleError=e.handleError,this.getUserInfo=e.getUserInfo,this.snowplow=e.snowplow,this.snowplowTrackers=this.arrayWrap(e.snowplowTrackers),this.validateArgs()}}export{r as EventLoggerImpl,e as NoopEventLogger,o as createEventLogger,s as getViewContexts}; //# sourceMappingURL=promoted-snowplow-logger.esm.js.map