!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.AlgoliaAnalytics=t()}(this,function(){"use strict";var e=function(e){return void 0===e},t=function(e){return"string"==typeof e},r=function(e){return"number"==typeof e},o=function(e){return"function"==typeof e};function n(n,i){if(!this._userHasOptedOut){if(!this._hasCredentials)throw new Error("Before calling any methods on the analytics, you first need to call the 'init' function with appId and apiKey parameters");if(!t(i.index))throw TypeError("expected required parameter `index` to be a string");if(!t(i.eventName))throw TypeError("expected required parameter `eventName` to be a string");if(!e(i.userToken)&&!t(i.userToken))throw TypeError("expected optional parameter `userToken` to be a string");var s={eventType:n,eventName:i.eventName,userToken:i.userToken||this._userToken,index:i.index};if(!e(i.timestamp)){if(!r(i.timestamp))throw TypeError("expected optional parameter `timestamp` to be a number");s.timestamp=i.timestamp}if(!e(i.queryID)){if(!t(i.queryID))throw TypeError("expected optional parameter `queryID` to be a string");s.queryID=i.queryID}if(!e(i.objectIDs)){if(!Array.isArray(i.objectIDs))throw TypeError("expected optional parameter `objectIDs` to be an array");s.objectIDs=i.objectIDs}if(!e(i.positions)){if(!Array.isArray(i.positions))throw TypeError("expected optional parameter `positions` to be an array");if(e(i.objectIDs))throw new Error("cannot use `positions` without providing `objectIDs`");if(i.objectIDs.length!==i.positions.length)throw new Error("objectIDs and positions need to be of the same size");s.positions=i.positions}if(!e(i.filters)){if(!e(i.objectIDs))throw new Error("cannot use `objectIDs` and `filters` for the same event");if(!Array.isArray(i.filters))throw TypeError("expected optional parameter `filters` to be an array");s.filters=i.filters}if(e(i.objectIDs)&&e(i.filters))throw new Error("expected either `objectIDs` or `filters` to be provided");!function(e,t,r,n,i){var s=n+"/1/events?X-Algolia-Application-Id="+e+"&X-Algolia-API-Key="+t+"&X-Algolia-Agent="+r,a=navigator&&o(navigator.sendBeacon),c={events:i};if(a)navigator.sendBeacon(s,JSON.stringify(c));else{var p=new XMLHttpRequest;p.open("POST",s),p.send(JSON.stringify(c))}}(this._appId,this._apiKey,this._uaURIEncoded,this._endpointOrigin,[s])}}var i="insights-js (1.1.1)";var s=["de","us"],a=2592e6;var c,p,u,h,d=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})},f="_ALGOLIA",l=function(e,t,r){var o=new Date;o.setTime(o.getTime()+r);var n="expires="+o.toUTCString();document.cookie=e+"="+t+";"+n+";path=/"},w=function(e){for(var t=e+"=",r=decodeURIComponent(document.cookie).split(";"),o=0;o<r.length;o++){for(var n=r[o];" "===n.charAt(0);)n=n.substring(1);if(0===n.indexOf(t))return n.substring(t.length,n.length)}return""},b="ANONYMOUS_USER_TOKEN";Object.keys||(Object.keys=(c=Object.prototype.hasOwnProperty,p=!{toString:null}.propertyIsEnumerable("toString"),h=(u=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"]).length,function(e){if("function"!=typeof e&&("object"!=typeof e||null===e))throw new TypeError("Object.keys called on non-object");var t,r,o=[];for(t in e)c.call(e,t)&&o.push(t);if(p)for(r=0;r<h;r++)c.call(e,u[r])&&o.push(u[r]);return o})),"function"!=typeof Object.assign&&(Object.assign=function(e,t){var r=arguments;if(null==e)throw new TypeError("Cannot convert undefined or null to object");for(var o=Object(e),n=1;n<arguments.length;n++){var i=r[n];if(null!=i)for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(o[s]=i[s])}return o});return new function(o){this._ua="",this._uaURIEncoded="",this.version="1.1.1",this._hasCredentials=!1,this.processQueue=function(){var e=this,t=window.AlgoliaAnalyticsObject;t&&((window[t].queue||[]).forEach(function(t){var r=t[0],o=t[1];r&&"function"==typeof e[r]&&e[r](o)}),window[t]=function(t,r){e[t](r)})}.bind(this),this.sendEvent=n.bind(this),this.init=function(o){if(!o)throw new Error("Init function should be called with an object argument containing your apiKey and appId");if(e(o.apiKey)||!t(o.apiKey))throw new Error("apiKey is missing, please provide it so we can authenticate the application");if(e(o.appId)||!t(o.appId))throw new Error("appId is missing, please provide it, so we can properly attribute data to your application");if(!e(o.region)&&-1===s.indexOf(o.region))throw new Error("optional region is incorrect, please provide either one of: "+s.join(", ")+".");if(!(e(o.cookieDuration)||r(o.cookieDuration)&&isFinite(o.cookieDuration)&&Math.floor(o.cookieDuration)===o.cookieDuration))throw new Error("optional cookieDuration is incorrect, expected an integer.");this._apiKey=o.apiKey,this._appId=o.appId,this._userHasOptedOut=!!o.userHasOptedOut,this._region=o.region,this._endpointOrigin=o.region?"https://insights."+o.region+".algolia.io":"https://insights.algolia.io",this._cookieDuration=o.cookieDuration?o.cookieDuration:6*a,this._hasCredentials=!0,this._ua=i,this._uaURIEncoded=encodeURIComponent(i)}.bind(this),this.initSearch=function(e){if(!this._hasCredentials)throw new Error("Before calling any methods on the analytics, you first need to call the 'init' function with appId and apiKey parameters");if(!e)throw new Error("initSearch function requires an argument with getQueryID and hitsContainer arguments");if(!e.getQueryID||"function"!=typeof e.getQueryID)throw new Error("getQueryID must be a function that returns the queryID of the last search operation");this.getQueryID=e.getQueryID}.bind(this),this.addAlgoliaAgent=function(e){-1===this._ua.indexOf("; "+e)&&(this._ua+="; "+e,this._uaURIEncoded=encodeURIComponent(this._ua))}.bind(this),this.ANONYMOUS_USER_TOKEN=b,this.setUserToken=function(e){if(e===b){var t=w(f);t&&""!==t&&t.startsWith("anonymous-")?this._userToken=t:(this._userToken="anonymous-"+d(),l(f,this._userToken,this._cookieDuration))}else this._userToken=e}.bind(this),this.getUserToken=function(){return this._userToken}.bind(this),this.clickedObjectIDsAfterSearch=function(e){if(!e)throw new Error("No params were sent to clickedObjectIDsAfterSearch function, please provide `queryID`,  `objectIDs` and `positions` to be reported");if(!e.queryID)throw new Error("required queryID parameter was not sent, click event can not be properly sent without");if(!e.objectIDs)throw new Error("required objectIDs parameter was not sent, click event can not be properly sent without");if(!e.positions)throw new Error("required positions parameter was not sent, click event can not be properly sent without");this.sendEvent("click",e)}.bind(this),this.clickedObjectIDs=function(e){if(!e)throw new Error("No params were sent to clickedObjectIDs function, please provide `objectIDs` to be reported");if(!e.objectIDs)throw new Error("required `objectIDs` parameter was not sent, click event can not be properly sent without");this.sendEvent("click",e)}.bind(this),this.clickedFilters=function(e){if(!e)throw new Error("No params were sent to clickedFilters function, please provide `filters` to be reported");if(!e.filters)throw new Error("required `filters` parameter was not sent, click event can not be properly sent without");this.sendEvent("click",e)}.bind(this),this.convertedObjectIDsAfterSearch=function(e){if(!e)throw new Error("No params were sent to convertedObjectIDsAfterSearch function, please provide `queryID` and `objectIDs` to be reported");if(!e.queryID)throw new Error("required queryID parameter was not sent, conversion event can not be properly sent without");if(!e.objectIDs)throw new Error("required objectIDs parameter was not sent, conversion event can not be properly sent without");this.sendEvent("conversion",e)}.bind(this),this.convertedObjectIDs=function(e){if(!e)throw new Error("No params were sent to convertedObjectIDs function, please provide `objectIDs` to be reported");if(!e.objectIDs)throw new Error("required objectIDs parameter was not sent, conversion event can not be properly sent without");this.sendEvent("conversion",e)}.bind(this),this.convertedFilters=function(e){if(!e)throw new Error("No params were sent to convertedFilters function, please provide `filters` to be reported");if(!e.filters)throw new Error("required filters parameter was not sent, conversion event can not be properly sent without");this.sendEvent("conversion",e)}.bind(this),this.viewedObjectIDs=function(e){if(!e)throw new Error("No params were sent to viewedObjectIDs function, please provide `objectIDs` to be reported");if(!e.objectIDs)throw new Error("required objectIDs parameter was not sent, view event can not be properly sent without");this.sendEvent("view",e)}.bind(this),this.viewedFilters=function(e){if(!e)throw new Error("No params were sent to viewedFilters function, please provide `filters` to be reported");if(!e.filters)throw new Error("required filters parameter was not sent, view event can not be properly sent without");this.sendEvent("view",e)}.bind(this),this.setUserToken(this.ANONYMOUS_USER_TOKEN),this.processQueue()}});
