UNPKG

@spindl-xyz/attribution

Version:
3 lines (2 loc) 15.5 kB
"use strict";var e="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",t=32;function r(){if("undefined"==typeof window||"undefined"==typeof crypto||void 0===crypto.getRandomValues)return function(){for(var r="",n=0;n<t;n++)r+=e.charAt(Math.floor(Math.random()*e.length));return r}();var r=crypto.getRandomValues(new Uint8Array(t));return Array.from(r,(function(t){return e[t%e.length]})).join("")}function n(){return"undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0}function o(){if("undefined"!=typeof window){var e=new URL(null===window||void 0===window?void 0:window.location.href),t=e.searchParams,r=function(e){if(""!==e){var t=e.indexOf("?");if(!(t<0||t>=e.length-1)){var r=e.substring(t+1),n=r.indexOf("#");return n>=0&&(r=r.substring(0,n)),new URLSearchParams(r)}}}(e.hash);return r&&r.forEach((function(e,r){t.has(r)||t.append(r,e)})),t}}var i=function(){return i=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},i.apply(this,arguments)};function a(e,t,r,n){return new(r||(r=Promise))((function(o,i){function a(e){try{s(n.next(e))}catch(e){i(e)}}function u(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,u)}s((n=n.apply(e,t||[])).next())}))}function u(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(u){return function(s){return function(u){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,u[0]&&(a=0)),a;)try{if(r=1,n&&(o=2&u[0]?n.return:u[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,u[1])).done)return o;switch(n=0,o&&(u=[2&u[0],o.value]),u[0]){case 0:case 1:o=u;break;case 4:return a.label++,{value:u[1],done:!1};case 5:a.label++,n=u[1],u=[0];continue;case 7:u=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==u[0]&&2!==u[0])){a=0;continue}if(3===u[0]&&(!o||u[1]>o[0]&&u[1]<o[3])){a.label=u[1];break}if(6===u[0]&&a.label<o[1]){a.label=o[1],o=u;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(u);break}o[2]&&a.ops.pop(),a.trys.pop();continue}u=t.call(e,a)}catch(e){u=[6,e],n=0}finally{r=o=0}if(5&u[0])throw u[1];return{value:u[0]?u[1]:void 0,done:!0}}([u,s])}}}function s(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,o,i=r.call(e),a=[];try{for(;(void 0===t||t-- >0)&&!(n=i.next()).done;)a.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(o)throw o.error}}return a}function c(e,t,r){if(r||2===arguments.length)for(var n,o=0,i=t.length;o<i;o++)!n&&o in t||(n||(n=Array.prototype.slice.call(t,0,o)),n[o]=t[o]);return e.concat(n||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var d={sdkKey:"",api:"https://api.spindl.xyz/v1",host:"https://spindl.link",maxRetries:3,debugMode:!1};var l="Spindl SDK";var f="object"==typeof window,v=function(){var e;return f?null===(e=null===window||void 0===window?void 0:window.location)||void 0===e?void 0:e.href:void 0},h=function(){var e;return f?null===(e=null===window||void 0===window?void 0:window.document)||void 0===e?void 0:e.referrer:void 0},g="";function p(e,t){if(void 0===t&&(t=document),g)return g;if(!t)return"";if(["localhost","127.0.0.1"].includes(e))return"";for(var n=e.split("."),o=Math.min(n.length,8),i="dmn_chk_"+r(),a=new RegExp("(^|;)\\s*"+i+"=1");!g&&o--;){var u=n.slice(o).join("."),s=i+"=1;domain=."+u;t.cookie=s,a.test(t.cookie)&&(t.cookie=s+";expires=Thu, 01 Jan 1970 00:00:00 GMT",g=u)}return g}function y(e,t,r,n){var o=new Date;o.setTime(o.getTime()+24*r*60*60*1e3);var i="expires="+o.toUTCString(),a=p(n.location.hostname,n);n.cookie="".concat(e,"=").concat(t,";").concat(i,";path=/;domain=").concat(a,";SameSite=Lax")}var w={length:0,clear:function(){},getItem:function(e){return null},key:function(e){return null},removeItem:function(e){},setItem:function(e,t){}};var b,m,S;var C,k,R,A,I,P,K=(C="1.8.1",k=function(e){var t,r,n=i(i(i({},d),e),{sdkKey:null!==(r=null!==(t=e.sdkKey)&&void 0!==t?t:e.API_KEY)&&void 0!==r?r:d.sdkKey});return{setConfig:function(e){var t,r,o=null!==(r=null!==(t=e.API_KEY)&&void 0!==t?t:e.sdkKey)&&void 0!==r?r:n.sdkKey;n=i(i(i({},n),e),{sdkKey:o})},getConfig:function(){return n}}}({sdkKey:""}),R=function(e){return{info:function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];e.getConfig().debugMode&&console.log.apply(console,c(["[".concat(l,"]")],s(t),!1))},warn:function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];e.getConfig().debugMode&&console.warn.apply(console,c(["[".concat(l,"]")],s(t),!1))},error:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.error.apply(console,c(["[".concat(l,"]")],s(e),!1))}}}(k),A=function(e,t){function r(e){return a(this,void 0,void 0,(function(){var r,n;return u(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,fetch(e.url,e)];case 1:return(r=o.sent()).ok?[2,r]:(t.error("request failed with http code:",r.status),[2,void 0]);case 2:return"Failed to fetch"===(n=o.sent()).message?[2,{stopRetry:!0}]:(t.error("request failed",n),[2,void 0]);case 3:return[2]}}))}))}function n(t){return a(this,void 0,void 0,(function(){var r,n;return u(this,(function(o){switch(o.label){case 0:r=0,o.label=1;case 1:return r<e.getConfig().maxRetries?[4,t()]:[3,4];case 2:if(!0===(null==(n=o.sent())?void 0:n.stopRetry))return[2];if(null==n?void 0:n.ok)return[2,n];o.label=3;case 3:return r++,[3,1];case 4:return[2,void 0]}}))}))}return{send:r,retrySend:function(e){return a(this,void 0,void 0,(function(){return u(this,(function(t){return[2,n((function(){return r(e)}))]}))}))},retryRequest:n}}(k,R),i(i({},k),{http:A,logger:R,version:C})),_=(I=[function(){if(void 0===n()||"undefined"==typeof sessionStorage)return{getValue:function(){return{}}};var e="spindl_session_id",t=sessionStorage.getItem(e)||"";return t||(t=r(),sessionStorage.setItem(e,t)),{getValue:function(){return{sessionId:t}}}},function(){if(void 0===n()||"undefined"==typeof localStorage)return{getValue:function(){return{}}};var e="spindl_referrer_code",t=localStorage.getItem(e)||"";return{getValue:function(){return a(this,void 0,void 0,(function(){var r,n;return u(this,(function(i){return r=o(),(n=r.get("ref"))&&n!==t&&(t=n,localStorage.setItem(e,t)),t?[2,{referrerCode:t}]:[2,{}]}))}))}}},function(){var e=n();if(function(e){var t=null!=e?e:n();return void 0===t||void 0===t.localStorage||void 0===t.sessionStorage}(e))return{getValue:function(){return{}}};var t="spindl_persistent_id",o="spdl_pid",i=function(e){for(var t=e+"=",r=void 0,n=document.cookie.split(";"),o=0;o<n.length;o++){var i=n[o].trim();0===i.indexOf(t)&&(r=i.substring(t.length,i.length))}return r}(o),a=e.localStorage.getItem(t),u=i||a||"";return!i&&a?y(o,u=a,365,e.document):u||(y(o,u=r(),365,e.document),e.localStorage.setItem(t,u)),{getValue:function(){return{persistentId:u}}}},function(){if(void 0===n()||"undefined"==typeof localStorage)return{getValue:function(){return{}}};var e=localStorage.getItem("spindl_id")||void 0,t=localStorage.getItem("spindl_prev_id")||void 0;return{getValue:function(){return{identity:e,prevIdentity:t}}}},function(){return{getValue:function(){return"undefined"==typeof screen?{}:{screen:[screen.width,screen.height,screen.colorDepth]}}}},function(){return{getValue:function(){return a(this,void 0,void 0,(function(){var e,t;return u(this,(function(r){switch(r.label){case 0:return void 0===(e=n())||"undefined"==typeof navigator?[2,{}]:"object"!=typeof(t=e.navigator).brave||"function"!=typeof t.brave.isBrave?[3,2]:[4,t.brave.isBrave()];case 1:if(r.sent())return[2,{features:[1]}];r.label=2;case 2:return[2,{}]}}))}))}}},function(){return{getValue:function(){return{ts:(new Date).getTime()}}}}],P=I.map((function(e){return e()})),{getMetadata:function(){return a(this,void 0,void 0,(function(){var e,t,r,n,o,i,a;return u(this,(function(u){switch(u.label){case 0:return[4,Promise.all(P.map((function(e){return e.getValue()})))];case 1:e=u.sent(),t={};try{for(r=function(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(e),n=r.next();!n.done;n=r.next())o=n.value,Object.assign(t,o)}catch(e){i={error:e}}finally{try{n&&!n.done&&(a=r.return)&&a.call(r)}finally{if(i)throw i.error}}return[2,t]}}))}))}}),V=function(e,t){var r,o,s=n();o=void 0===s||"undefined"==typeof sessionStorage?w:sessionStorage;var c="spindl_request_id",d=parseInt(null!==(r=o.getItem(c))&&void 0!==r?r:"0");function l(){var e=d+1;return o.setItem(c,e.toString()),d++}return{sendBeacon:function(r){return a(this,void 0,void 0,(function(){var n,o,a,s,c;return u(this,(function(u){switch(u.label){case 0:return n={"X-API-Key":e.getConfig().sdkKey},[4,t.getMetadata()];case 1:return o=u.sent(),a=l(),s=i(i({},r),{metadata:i(i({version:e.version},o),{reqId:a})}),[4,e.http.retrySend({url:"".concat(e.getConfig().host,"/events"),headers:i(i({},n),{"content-type":"application/json"}),method:"POST",credentials:"include",body:JSON.stringify(s)})];case 2:return(c=u.sent())&&e.logger.info("sent ".concat(s.type)),[2,Boolean(c)]}}))}))}}}(K,_),x=(b=function(e){var t=!1;function r(){if(void 0!==n()&&"undefined"!=typeof document&&"undefined"!=typeof location){var t=v(),r=h();return e.sendBeacon({type:"PAGE_VIEW",data:{sourceUrl:t,referrer:r}})}}return{pageView:r,enableAutoPageViews:function(e){var o=(e||{}).trackReplaceState,i=void 0!==o&&o;if(void 0===n()||"undefined"==typeof history||!0===t)return function(){};t=!0,r();var a=function(){return r()},u=history.pushState,s=history.replaceState;return u&&s&&(history.pushState=function(e,t,r){u.call(this,e,t,r),a()},history.replaceState=function(e,t,r){s.call(this,e,t,r),i&&a()},addEventListener("popstate",a)),function(){u&&(history.pushState=u,history.replaceState=s,removeEventListener("popstate",a))}}}}(V),b.pageView),E=b.enableAutoPageViews,j=(m=function(e){function t(t){var r=v(),n=h();return e.sendBeacon({type:"WALLET_CONNECTED",data:{address:t,sourceUrl:r,referrer:n}})}return{attribute:t,enableAutoWalletConnects:function(){var e,r,n=(null===window||void 0===window?void 0:window.ethereum)||(null===(e=window.web3)||void 0===e?void 0:e.currentProvider),o=function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var o=e[0];o.length>0&&o[0]!==r?(t(o[0]),r=o[0]):r=void 0};return null==n||n.on("accountsChanged",o),addEventListener("load",(function(e){var n,o,i,a=(null===(n=null===window||void 0===window?void 0:window.ethereum)||void 0===n?void 0:n.selectedAddress)||(null===(i=null===(o=window.web3)||void 0===o?void 0:o.currentProvider)||void 0===i?void 0:i.selectedAddress);a&&r!==a&&(r=a,t(a))})),function(){n&&n.removeListener("accountsChanged",o)}}}}(V),m.attribute),T=m.enableAutoWalletConnects,O=(S=function(e,t){function r(t){return void 0===t&&(t={}),a(this,void 0,void 0,(function(){var r,n;return u(this,(function(o){switch(o.label){case 0:return r=new URL("".concat(e.getConfig().api,"/referrer-codes")),t.address&&r.searchParams.set("address",t.address),t.code&&r.searchParams.set("code",t.code),[4,e.http.send({url:r.toString(),headers:{"X-API-Key":e.getConfig().sdkKey},method:"GET"})];case 1:return(n=o.sent()).ok?[2,n.json()]:(e.logger.error("request failed with http code:",n.status),[2,[]])}}))}))}return{getReferrerAttribution:function(r){return a(this,void 0,void 0,(function(){var n,o,i;return u(this,(function(a){switch(a.label){case 0:return n=new URL("".concat(e.getConfig().api,"/referrer-attribution/").concat(r)),[4,t.getMetadata()];case 1:return"string"==typeof(o=a.sent()).referrerCode&&n.searchParams.set("ref",o.referrerCode),[4,e.http.send({url:n.toString(),headers:{"X-API-Key":e.getConfig().sdkKey},method:"GET"})];case 2:return(null==(i=a.sent())?void 0:i.ok)?[4,i.json()]:(e.logger.error("request failed with http code:",i.status),[2,void 0]);case 3:return[2,a.sent().attribution]}}))}))},getReferrerStats:function(t){return a(this,void 0,void 0,(function(){var r,n;return u(this,(function(o){switch(o.label){case 0:return r=new URL("".concat(e.getConfig().api,"/referrals/stats/").concat(t)),[4,e.http.send({url:r.toString(),headers:{"X-API-Key":e.getConfig().sdkKey},method:"GET"})];case 1:return(null==(n=o.sent())?void 0:n.ok)?[4,n.json()]:(e.logger.error("request failed with http code:",n.status),[2,void 0]);case 2:return[2,o.sent()]}}))}))},getReferrerHistory:function(t){return a(this,void 0,void 0,(function(){var r,n;return u(this,(function(o){switch(o.label){case 0:return r=new URL("".concat(e.getConfig().api,"/referrals/history/").concat(t)),[4,e.http.send({url:r.toString(),headers:{"X-API-Key":e.getConfig().sdkKey},method:"GET"})];case 1:return(null==(n=o.sent())?void 0:n.ok)?[4,n.json()]:(e.logger.error("request failed with http code:",n.status),[2,void 0]);case 2:return[2,o.sent().items]}}))}))},createReferrerCode:function(t,r){return a(this,void 0,void 0,(function(){var n;return u(this,(function(o){switch(o.label){case 0:return[4,e.http.send({url:"".concat(e.getConfig().api,"/referrer-codes"),headers:{"X-API-Key":e.getConfig().sdkKey,"content-type":"application/json"},method:"POST",body:JSON.stringify({address:t,code:r})})];case 1:return(n=o.sent()).ok?[4,n.json()]:(e.logger.error("request failed with http code:",n.status),[2,void 0]);case 2:return[2,o.sent().code]}}))}))},validateReferrerCode:function(e){return"string"==typeof e&&/^([a-z0-9_]){4,20}$/.test(e)},checkReferrerCode:function(t){return a(this,void 0,void 0,(function(){var r;return u(this,(function(n){switch(n.label){case 0:return[4,e.http.send({url:"".concat(e.getConfig().api,"/referrer-codes/check-availability"),headers:{"X-API-Key":e.getConfig().sdkKey,"content-type":"application/json"},method:"POST",body:JSON.stringify({code:t})})];case 1:return(r=n.sent()).ok?[4,r.json()]:(e.logger.error("request failed with http code:",r.status),[2,!1]);case 2:return[2,n.sent().available]}}))}))},getReferrerCode:function(e){return a(this,void 0,void 0,(function(){var t;return u(this,(function(n){switch(n.label){case 0:return e?[4,r({address:e})]:[2];case 1:return(null==(t=n.sent())?void 0:t.length)?[2,t[0].code]:[2]}}))}))},getReferrerAddress:function(e){return a(this,void 0,void 0,(function(){var t;return u(this,(function(n){switch(n.label){case 0:return e?[4,r({code:e})]:[2];case 1:return(null==(t=n.sent())?void 0:t.length)?[2,t[0].address]:[2]}}))}))}}}(K,_),S.checkReferrerCode),L=S.createReferrerCode,U=S.getReferrerAddress,q=S.getReferrerAttribution,M=S.getReferrerHistory,B=S.getReferrerStats,G=S.getReferrerCode,X=S.validateReferrerCode,D=function(e){return{track:function(t,r,n){var o=v();return e.sendBeacon({type:"CUSTOM",data:{name:t,properties:r,sourceUrl:o},identity:n})}}}(V).track;var N=Object.freeze({__proto__:null,attribute:j,checkReferrerCode:O,configure:function(e){K.setConfig(e)},createReferrerCode:L,enableAutoPageViews:E,enableAutoWalletConnects:T,getReferrerAddress:U,getReferrerAttribution:q,getReferrerCode:G,getReferrerHistory:M,getReferrerStats:B,pageView:x,track:D,validateReferrerCode:X});module.exports=N; //# sourceMappingURL=attribution.cjs.js.map