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.61 kB
JavaScript
!function(e,o){"object"==typeof exports&&"undefined"!=typeof module?o(exports):"function"==typeof define&&define.amd?define(["exports"],o):o((e="undefined"!=typeof globalThis?globalThis:e||self)["promoted-snowplow-logger"]={})}(this,(function(e){"use strict";class o{constructor(){this.logCohortMembership=()=>{},this.logView=()=>{},this.logImpression=()=>{},this.logAction=()=>{},this.logClick=()=>{},this.flushEarlyEvents=()=>{}}}const t=e=>e?[{schema:"iglu:ai.promoted/pageview_cx/jsonschema/2-0-0",data:e}]:[],s=e=>new Error(`${e} is required`);class r{constructor(e){this.validateArgs=()=>{if(!this.handleError)throw s("handleError");if(!this.snowplow)throw s("snowplow");if(!this.snowplow.trackSelfDescribingEvent)throw s("trackSelfDescribingEvent");if(!this.snowplow.trackPageView)throw s("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:t(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:o}=e;if(o)for(const e of o.contents){0==e.quantity&&this.handleError(new Error("CartContent.quantity should be non-zero"));const{pricePerUnit:o}=e;o&&void 0!==o.amount&&(void 0!==o.amountMicros&&this.handleError(new Error("Do not set both amount and amountMicros")),e.pricePerUnit={currencyCode:o.currencyCode,amountMicros:Math.round(1e6*o.amount)})}},this.logClick=e=>{const o=Object.assign({actionType:2},e),{targetUrl:t}=e;delete o.targetUrl,t&&(o.navigateAction={targetUrl:t}),this.logAction(o)},this.mergeBaseUserInfo=e=>{var o;const t=null===(o=this.getUserInfo)||void 0===o?void 0:o.call(this);if(t){const{userInfo:o}=e;e.userInfo=Object.assign(Object.assign({},t),o)}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()}}e.EventLoggerImpl=r,e.NoopEventLogger=o,e.createEventLogger=e=>void 0===e.enabled||e.enabled?new r(e):new o,e.getViewContexts=t}));
//# sourceMappingURL=promoted-snowplow-logger.umd.js.map