!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).AlgoliaAnalytics=t()}(this,function(){"use strict";var e=function(){try{return Boolean(navigator.cookieEnabled)}catch(e){return!1}},t=function(){try{return Boolean(navigator.sendBeacon)}catch(e){return!1}},r=function(){try{return Boolean(XMLHttpRequest)}catch(e){return!1}},n=function(e){return void 0===e},o=function(e){return"string"==typeof e},i=function(e){return"number"==typeof e},s=function(e){return"function"==typeof e};var a="insights-js (1.3.0)";var c=["de","us"],u=2592e6;var p,h,f,l,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)})},w="_ALGOLIA",y=function(e,t,r){var n=new Date;n.setTime(n.getTime()+r);var o="expires="+n.toUTCString();document.cookie=e+"="+t+";"+o+";path=/"},b=function(e){for(var t=e+"=",r=document.cookie.split(";"),n=0;n<r.length;n++){for(var o=r[n];" "===o.charAt(0);)o=o.substring(1);if(0===o.indexOf(t))return o.substring(t.length,o.length)}return""},v="ANONYMOUS_USER_TOKEN";Object.keys||(Object.keys=(p=Object.prototype.hasOwnProperty,h=!{toString:null}.propertyIsEnumerable("toString"),l=(f=["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,n=[];for(t in e)p.call(e,t)&&n.push(t);if(h)for(r=0;r<l;r++)p.call(e,f[r])&&n.push(f[r]);return n})),"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 n=Object(e),o=1;o<arguments.length;o++){var i=r[o];if(null!=i)for(var s in i)Object.prototype.hasOwnProperty.call(i,s)&&(n[s]=i[s])}return n});var I=function(e,t){var r=JSON.stringify(t);navigator.sendBeacon(e,r)},m=function(e,t){var r=JSON.stringify(t),n=new XMLHttpRequest;n.open("POST",e),n.send(r)};var g=new function(t){var r=t.requestFn;this._ua="",this._uaURIEncoded="",this.version="1.3.0",this._hasCredentials=!1,this.sendEvent=function(e){return function(t,r){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(""===r.userToken||""===this._userToken)throw new Error("`userToken` cannot be an empty string.");var s=r.userToken||this._userToken;if(n(s))throw new Error("Before calling any methods on the analytics, you first need to call 'setUserToken' function or include 'userToken' in the event payload.");if(!o(r.index))throw new TypeError("expected required parameter `index` to be a string");if(!o(r.eventName))throw new TypeError("expected required parameter `eventName` to be a string");if(!n(r.userToken)&&!o(r.userToken))throw new TypeError("expected optional parameter `userToken` to be a string");var a={eventType:t,eventName:r.eventName,userToken:s,index:r.index};if(!n(r.timestamp)){if(!i(r.timestamp))throw new TypeError("expected optional parameter `timestamp` to be a number");a.timestamp=r.timestamp}if(!n(r.queryID)){if(!o(r.queryID))throw new TypeError("expected optional parameter `queryID` to be a string");a.queryID=r.queryID}if(!n(r.objectIDs)){if(!Array.isArray(r.objectIDs))throw new TypeError("expected optional parameter `objectIDs` to be an array");a.objectIDs=r.objectIDs}if(!n(r.positions)){if(!Array.isArray(r.positions))throw new TypeError("expected optional parameter `positions` to be an array");if(n(r.objectIDs))throw new Error("cannot use `positions` without providing `objectIDs`");if(r.objectIDs.length!==r.positions.length)throw new Error("objectIDs and positions need to be of the same size");a.positions=r.positions}if(!n(r.filters)){if(!n(r.objectIDs))throw new Error("cannot use `objectIDs` and `filters` for the same event");if(!Array.isArray(r.filters))throw new TypeError("expected optional parameter `filters` to be an array");a.filters=r.filters}if(n(r.objectIDs)&&n(r.filters))throw new Error("expected either `objectIDs` or `filters` to be provided");!function(e,t,r,n,o,i){e(o+"/1/events?X-Algolia-Application-Id="+t+"&X-Algolia-API-Key="+r+"&X-Algolia-Agent="+n,{events:i})}(e,this._appId,this._apiKey,this._uaURIEncoded,this._endpointOrigin,[a])}}}(r).bind(this),this.init=function(t){if(!t)throw new Error("Init function should be called with an object argument containing your apiKey and appId");if(n(t.apiKey)||!o(t.apiKey))throw new Error("apiKey is missing, please provide it so we can authenticate the application");if(n(t.appId)||!o(t.appId))throw new Error("appId is missing, please provide it, so we can properly attribute data to your application");if(!n(t.region)&&-1===c.indexOf(t.region))throw new Error("optional region is incorrect, please provide either one of: "+c.join(", ")+".");if(!(n(t.cookieDuration)||i(t.cookieDuration)&&isFinite(t.cookieDuration)&&Math.floor(t.cookieDuration)===t.cookieDuration))throw new Error("optional cookieDuration is incorrect, expected an integer.");this._apiKey=t.apiKey,this._appId=t.appId,this._userHasOptedOut=!!t.userHasOptedOut,this._region=t.region,this._endpointOrigin=t.region?"https://insights."+t.region+".algolia.io":"https://insights.algolia.io",this._cookieDuration=t.cookieDuration?t.cookieDuration:6*u,this._hasCredentials=!0,this._ua=a,this._uaURIEncoded=encodeURIComponent(a),e()&&this.setUserToken(this.ANONYMOUS_USER_TOKEN)}.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=v,this.setUserToken=function(t){if(t===v){if(!e())throw new Error("Tracking of anonymous users is only possible on environments which support cookies.");var r=b(w);r&&""!==r&&0===r.indexOf("anonymous-")?this._userToken=r:(this._userToken="anonymous-"+d(),y(w,this._userToken,this._cookieDuration))}else this._userToken=t}.bind(this),this.getUserToken=function(e,t){return s(t)&&t(null,this._userToken),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)}({requestFn:function(){if(t())return I;if(r())return m;throw new Error("Could not find a supported HTTP request client in this environment.")}()});return"undefined"!=typeof window&&function(e){var t,r=e.AlgoliaAnalyticsObject;if(r){var n=(t=this,function(e){for(var r=[],n=arguments.length-1;n-- >0;)r[n]=arguments[n+1];e&&s(t[e])&&t[e].apply(t,r)}),o=e[r];o.queue=o.queue||[];var i=o.queue;i.forEach(function(e){var t=[].slice.call(e),r=t[0],o=t.slice(1);n.apply(void 0,[r].concat(o))}),i.push=function(e){var t=[].slice.call(e),r=t[0],o=t.slice(1);n.apply(void 0,[r].concat(o))}}}.call(g,window),g});
