-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsegment.min.js
More file actions
1 lines (1 loc) · 6.24 KB
/
segment.min.js
File metadata and controls
1 lines (1 loc) · 6.24 KB
1
angular.module("ngSegment",[]),angular.module("ngSegment").constant("segmentDefaultConfig",{apiKey:null,autoload:!0,loadDelay:0,condition:null,debug:!1,logger:"$log",debugLevel:"log",methods:["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"],tag:"[ngSegment] "}),function(a){function b(a){this.hasLoaded=a||!1,this.load=function(a,b){if(window.analytics.initialized&&console.warn("Warning: Segment analytics has already been initialized. Did you already load the library?"),this.hasLoaded)throw new Error("Attempting to load Segment twice.");if(!a)throw new Error("Cannot load Analytics.js without an API key.");this.hasLoaded=!0,window.setTimeout(function(){var b=document.createElement("script");b.type="text/javascript",b.async=!0,b.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+a+"/analytics.min.js",b.onerror=function(){console.error("Error loading Segment library.")};var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)},b)}}function c(){b.call(this),this.$get=function(){return new b(this.hasLoaded)}}a.provider("segmentLoader",c)}(angular.module("ngSegment")),function(a){function b(a){this.config=a,this.factory=function(a){var b=this;return function(){return b.config.condition&&!b.config.condition(a,arguments)?void b.debug("Not calling method, condition returned false.",{method:a,arguments:arguments}):(b.debug("Calling method "+a+" with arguments:",arguments),window.analytics[a].apply(d,arguments))}}}function c(a){this.config=angular.copy(a),this.queue=[],this.factory=function(a){var b=this.queue;return function(){b.push({method:a,arguments:arguments})}},this.init(),this.setKey=function(a){return this.config.apiKey=a,this.validate("apiKey"),this},this.setLoadDelay=function(a){return this.config.loadDelay=a,this.validate("loadDelay"),this},this.setCondition=function(a){return this.config.condition=a,this.validate("condition"),this},this.setLogger=function(a){return this.config.logger=a,this.validate("logger"),this},this.setDebugLevel=function(a){return this.config.debugLevel=a,this.validate("debugLevel"),this},this.setEvents=function(a){return this.events=a,this},this.setConfig=function(a){if(!angular.isObject(a))throw new Error(this.config.tag+"Config must be an object.");angular.extend(this.config,a);var b=this;return Object.keys(a).forEach(function(a){b.validate(a)}),this},this.setAutoload=function(a){return this.config.autoload=!!a,this},this.setDebug=function(a){return this.config.debug=!!a,this};var c={apiKey:function(a){if(!angular.isString(a.apiKey)||!a.apiKey)throw new Error(a.tag+"API key must be a valid string.")},loadDelay:function(a){if(!angular.isNumber(a.loadDelay))throw new Error(a.tag+"Load delay must be a number.")},condition:function(a){if(!(angular.isFunction(a.condition)||angular.isArray(a.condition)&&angular.isFunction(a.condition[a.condition.length-1])))throw new Error(a.tag+"Condition callback must be a function or array.")},logger:function(a){if(!angular.isFunction(a.logger)&&!angular.isObject(a.logger)&&!angular.isString(a.logger)&&null!=a.logger)throw new Error(a.tag+"Logger must be either an object or a string or a function or undefined/null.");var b=typeof a.debugLevel;if(angular.isObject(a.logger)){if("string"!==b)throw new Error(a.tag+"Logger given as an object requires specifying `debugLevel` config as a string. `debugLevel` was a `"+b+"`");if(!(a.debugLevel in a.logger))throw new Error(a.tag+"Logger given as an object does not have a method called `"+a.debugLevel+"` (set as `debugLevel`). Methods in the given object are: `"+Object.keys(a.logger).filter(function(b){return angular.isFunction(a.logger[b])}).join(", ")+"`")}if(angular.isFunction(a.logger)&&"string"!==b)throw new Error(a.tag+"Logger given as a function requires specifying `debugLevel` config as a string. `debugLevel` was a `"+b+"`")},debugLevel:function(a){if(!angular.isString(a.debugLevel))throw new Error(a.tag+"`debugLevel` must be a string.")}};this.validate=function(a){"function"==typeof c[a]&&c[a](this.config)},this.createService=function(a,c){var d=this,e=[];if(a.has("segmentConfig")){var f=a.get("segmentConfig");if(!angular.isObject(f))throw new Error(this.config.tag+"Config constant must be an object.");angular.extend(this.config,f),e.push("Found segment config constant");var g=this;Object.keys(f).forEach(function(a){g.validate(a)})}if("logger"in this.config&&this.config.logger)switch(typeof this.config.logger){case"string":if(!a.has(this.config.logger))throw new Error(this.config.tag+"Logger service `"+this.config.logger+"` is not known.");this.logger=a.get(this.config.logger);break;case"object":this.logger=this.config.logger;break;case"function":this.logger={},this.logger[this.config.debugLevel]=this.config.logger.bind(void 0)}else this.logger=a.get("$log");if(e.forEach(function(a){d.debug(a)}),this.config.autoload&&(this.debug("Autoloading Analytics.js"),this.config.apiKey?c.load(this.config.apiKey,this.config.loadDelay):this.debug(this.config.tag+" Warning: API key is not set and autoload is not disabled.")),"function"==typeof this.config.condition||"array"==typeof this.config.condition&&"function"==typeof this.config.condition[this.config.condition-1]){var h=this.config.condition;this.config.condition=function(b,c){return a.invoke(h,h,{method:b,params:c})}}var i=new b(angular.copy(this.config));return this.events&&(i.events=angular.copy(this.events)),i.init(),this.queue.forEach(function(a){i[a.method].apply(i,a.arguments)}),i.debug=function(){return b.prototype.debug.apply(d,arguments)},i},this.$get=["$injector","segmentLoader",this.createService]}var d=window.analytics=window.analytics||[];d.invoked?console.error("Segment or ngSegment included twice."):d.invoked=!0,d.factory=function(a){return function(){var b=Array.prototype.slice.call(arguments);return b.unshift(a),d.push(b),d}},b.prototype={init:function(){for(var a=0;a<this.config.methods.length;a++){var b=this.config.methods[a];d[b]||(d[b]=d.factory(b)),this[b]=this.factory(b)}},debug:function(){if(this.config.debug)return arguments[0]=this.config.tag+arguments[0],this.logger[this.config.debugLevel].apply(this.logger,arguments),!0}},c.prototype=Object.create(b.prototype),a.provider("segment",["segmentDefaultConfig",c])}(angular.module("ngSegment"));