@kameleoon/javascript-sdk-core
Advanced tools
Comparing version 5.1.2 to 5.1.3
# Change Log | ||
## 5.1.3 | ||
### Patch Changes | ||
- Fixed the issue with `off` variation not being added to `getEngineTrackingCode` result | ||
## 5.1.2 (2024-12-05) | ||
@@ -4,0 +10,0 @@ |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonClient=void 0;var _tsRes=require("ts-res"),_cacheManager=require("./cacheManager"),_clientConfiguration=require("./clientConfiguration"),_clientSettings=require("./clientSettings"),_constants=require("./constants"),_kameleoonData=require("./kameleoonData"),_kameleoonError=require("./kameleoonError"),_requester=require("./requester"),_storage=require("./storage"),_types=require("./types"),_utilities=require("./utilities"),_variationConfiguration=require("./variationConfiguration"),_eventSource=require("./eventSource"),_visitorCodeManager=require("./visitorCodeManager"),_eventManager=require("./eventManager"),_tracking=require("./tracking"),_logging=require("./logging");function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class KameleoonClient{constructor(a){var b;let{siteCode:c,dependencies:d,configuration:e,internalConfiguration:f}=a;_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"clientConfiguration",void 0),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"consentDataStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"initialized",!1),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"tracker",void 0),_defineProperty(this,"environment",void 0);const g=null===f||void 0===f?void 0:f.externals.externalLogger;g&&_logging.KameleoonLogger.setLogger(g),_logging.KameleoonLogger.debug`CALL: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e}, internalConfiguration: ${f})`;const{settings:h}=new _clientSettings.ClientSettings(c,e);this.environment=h.environment;const{externalStorage:i,externalEventSource:j,externalPackageInfo:k,externalVisitorCodeManager:l,externalRequester:m,externalPRNG:n}=f.externals,{useAbortController:o}=f.settings,p=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),q=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ClientData),r=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TargetingData),s=new _storage.DataStorage(i,_storage.KameleoonStorageKey.DataInfo),t=new _storage.DataStorage(i,_storage.KameleoonStorageKey.VariationData),u=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ConsentData),v=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TrackingData),w=new _kameleoonData.DataManager({dataStorage:r,infoStorage:s,cleanupInterval:h.cleanupInterval}),x=null!==(b=null===d||void 0===d?void 0:d.urlProvider)&&void 0!==b?b:new _requester.UrlProvider;x.initialize({domain:h.networkDomain,siteCode:h.siteCode,packageInfo:k,environment:h.environment});const y=new _requester.Requester({urlProvider:x,useAbortController:o,packageInfo:k,externalRequester:m,requestTimeout:h.requestTimeout,trackRetryDelay:h.trackRetryDelay}),z=new _eventSource.KameleoonEventSource({siteCode:h.siteCode,externalEventSource:j,urlProvider:x}),A=new _eventManager.EventManager,B=new _clientConfiguration.ClientConfiguration({urlProvider:x,storage:q,updateInterval:h.updateInterval,dataManager:w,eventManager:A,requester:y,eventSource:z,externalVisitorCodeManager:l}),C=new _variationConfiguration.VariationConfiguration(t,l),D=new _tracking.Tracker({dataManager:w,trackingStorage:v,variationConfiguration:C,requester:y,trackingInterval:h.trackingInterval,prng:n});this.tracker=D,this.variationConfiguration=C,this.requester=y,this.trackingCache=p,this.dataManager=w,this.clientConfiguration=B,this.externalPackageInfo=k,this.consentDataStorage=u,this.visitorCodeManager=l,this.eventManager=A,_logging.KameleoonLogger.debug`RETURN: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e}, internalConfiguration: ${f})`}async initialize(){_logging.KameleoonLogger.info("CALL: KameleoonClient.initialize()");const a=await this.clientConfiguration.initialize();return a.throw(),this.initialized=!0,_logging.KameleoonLogger.info`RETURN: KameleoonClient.initialize() -> (result: ${a.ok})`,a.ok}addData(a){for(var b=arguments.length,c=Array(1<b?b-1:0),d=1;d<b;d++)c[d-1]=arguments[d];if(_logging.KameleoonLogger.info`CALL: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`,this.visitorCodeManager.validateVisitorCode(a).throw(),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.dataManager.storeData(a,...c).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`}async getRemoteVisitorData(a){let{visitorCode:b,shouldAddData:c=!0,filters:d=_constants.DEFAULT_VISITOR_DATA_FILTERS}=a;if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(b).throw();const{previousVisitAmount:e}=d;if("number"==typeof e&&(1>e||25<e))throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitAmount);const f=this.dataManager.isUniqueIdentifier(b),g=await this.requester.getVisitorData({visitorCode:b,filters:d,isMappingIdentifier:f}),h=g.throw(),{visitorData:i,storageVisitorData:j,visitsData:k}=_utilities.Parser.parseVisitorData({data:h,filters:d,visitorCode:b,dataManager:this.dataManager,variationConfiguration:this.variationConfiguration});for(const d of j)(c||this.dataManager.isPersistentCustomData(d))&&this.addData(b,d);return k&&c&&this.addData(b,k),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d}) -> (visitorData: ${i})`,i}trackConversion(a){let{visitorCode:b,goalId:c,revenue:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`,this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`}flush(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.flush(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a)return this.visitorCodeManager.validateVisitorCode(a).throw(),this.tracker.scheduleVisitor(a,this._isConsentProvided(a)),void _logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`;for(const b of this.dataManager.unsentDataVisitors)this.tracker.scheduleVisitor(b,this._isConsentProvided(b));_logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`}getFeatureFlags(){if(_logging.KameleoonLogger.info("CALL: KameleoonClient.getFeatureFlags()"),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const a=this.clientConfiguration.featureFlags,b=[];return a.forEach(a=>{let{id:c,featureKey:d}=a;b.push({id:c,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlags() -> (FeatureFlags: ${b})`,b}getVisitorFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const b=this._getActiveFeatureVariations(a),c=[];return b.forEach(a=>{let{featureFlagId:b,featureKey:d}=a;c.push({id:b,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getVariation(a){let{visitorCode:b,featureKey:c,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`;const e=this._getFeatureVariation({visitorCode:b,featureKey:c,track:d}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${e})`,e}getVariations(a){let{visitorCode:b,onlyActive:c=!1,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d})`;const e=new Map,f=this.clientConfiguration.featureFlags;for(const g of f.values()){const a=this._getFeatureVariation({visitorCode:b,featureKey:g.featureKey,track:d});a.ok&&(!c||a.data.key!==_constants.OFF_VARIATION_KEY)&&e.set(g.featureKey,a.data)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d}) -> (variations: ${e})`,e}getActiveFeatureFlags(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a})`;const b=this._getActiveFeatureVariations(a),c=new Map;return b.forEach(b=>{const{variationKey:d,featureKey:e,variationId:f,experimentId:g}=b;let h=[];null!==f&&null!==g&&(h=this._getFeatureVariables({visitorCode:a,featureKey:e,variationKey:d}));const i=h.map(a=>_utilities.Parser.parseFeatureVariable(a).throw());c.set(b.featureKey,{key:d,id:f,experimentId:g,variables:i})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getFeatureFlagVariationKey(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b}) -> (variationKey: ${c})`,c}getFeatureFlagVariable(a){let{visitorCode:b,featureKey:c,variableKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const{key:e}=this._getFeatureVariation({visitorCode:b,featureKey:c,track:!0}).throw(),f=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:e}),g=f.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariableNotFound,d,b);const{type:h,value:i}=_utilities.Parser.parseFeatureVariable(g).throw(),j={type:h,value:i};return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d}) -> (FeatureFlagVariable: ${j})`,j}getFeatureFlagVariables(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw(),d=this._getFeatureVariables({visitorCode:a,featureKey:b,variationKey:c}),e=[];for(const c of d){const a=_utilities.Parser.parseFeatureVariable(c).throw();e.push(a)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b}) -> (FeatureFlagVariables: ${e})`,e}isFeatureFlagActive(a,b){let[c,d,e]=["","",!0];"string"==typeof a?(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d})`,c=a,d=b):(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track ${e})`,[c,d]=[a.visitorCode,a.featureKey],a.track!==void 0&&(e=a.track)),this.visitorCodeManager.validateVisitorCode(c).throw();try{const{key:a}=this._getFeatureVariation({visitorCode:c,featureKey:d,track:e}).throw(),b=a!==_constants.OFF_VARIATION_KEY;return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: ${b})`,b}catch(a){if(a instanceof _kameleoonError.KameleoonError&&a.type===_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: false)`,!1;throw a}}async getRemoteData(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteData(key: ${a})`;const b=await this.requester.getRemoteData(a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteData(key: ${a}) -> (remoteData: ${b})`,b.throw()}async getVisitorWarehouseAudience(a){let{visitorCode:b,customDataIndex:c,warehouseKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=await this.getRemoteData(d||b);if(!e)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No data found for ${d||b} key`);if(!e[_constants.WAREHOUSE_AUDIENCE_KEY]||0===Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]).length)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: null)`,null;const f=new _kameleoonData.CustomData(c,...Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]));return this.dataManager.storeData(b,f),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: ${f})`,f}onEvent(a,b){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.onEvent(event: ${a}, callback)`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.eventManager.addEventHandler(a,b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.onEvent(event: ${a}, callback)`}getEngineTrackingCode(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getEngineTrackingCode(visitorCode: ${a})`,this.visitorCodeManager.validateVisitorCode(a).throw();const b=_utilities.Utilities.getTrackingCode(this.trackingCache,a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getEngineTrackingCode(visitorCode: ${a}) -> (trackingCode: ${b})`,b}isInitialized(){return _logging.KameleoonLogger.debug`CALL/RETURN: KameleoonClient.isInitialized() -> (initialized: ${this.initialized})`,this.initialized}setLogLevel(a){_logging.KameleoonLogger.setLogLevel(a)}setUserConsent(a){let{visitorCode:b,consent:c,setData:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this.consentDataStorage.update({[b]:c}),c?d({visitorCode:b,key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.DEFAULT_MAX_AGE,path:_visitorCodeManager.PATH}):this.visitorCodeManager.consentRequired&&d({visitorCode:"",key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.ZERO_MAX_AGE,path:_visitorCodeManager.PATH}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`}_isConsentProvided(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._isConsentProvided(visitorCode: ${a})`;const{isConsentRequired:b}=this.clientConfiguration,c=this.consentDataStorage.read(),d=!b||c.ok&&c.data[a];return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._isConsentProvided(visitorCode: ${a}) -> (isConsentProvided: ${d})`,d}_getFeatureVariables(a){let{visitorCode:b,featureKey:c,variationKey:d}=a;_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d})`;const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c);const g=f.variations.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d}) -> (variables: ${g.variables})`,g.variables}_getActiveFeatureVariations(a){if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const b=this.clientConfiguration.featureFlags,c=[],d=this.dataManager.getVisitorData(a),e=this.dataManager.getVisitorIdentifier(a);for(const f of b.values()){if(!f.environmentEnabled)continue;const b=this.variationConfiguration.getVariation({visitorCode:a,visitorIdentifier:e,targetingData:d,featureFlag:f,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:f.featureKey,variation:{key:b.variationKey,experimentId:b.experimentId,id:b.variationId}}),b.variationKey!==_constants.OFF_VARIATION_KEY&&c.push(b)}return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a}) -> (activeVariations: ${c})`,c}_getFeatureVariation(a){let{visitorCode:b,featureKey:c,track:d}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,!this.initialized)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c));if(!f.environmentEnabled)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled,f.featureKey,this.environment));const g=this.dataManager.getVisitorData(b),h=this.dataManager.getVisitorIdentifier(b),i=this.variationConfiguration.getVariation({visitorCode:b,visitorIdentifier:h,featureFlag:f,track:d,withAssignment:!0,targetingData:g,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:c,variation:{key:i.variationKey,experimentId:i.experimentId,id:i.variationId}});const{experimentId:j,variationId:k,variationKey:l}=i;let m=new Map,n=!1;try{const a=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:l});a.forEach(a=>{n||a.type!==_types.VariableType.JS&&a.type!==_types.VariableType.CSS||(n=!0);const b=_utilities.Parser.parseFeatureVariable(a).throw();m.set(a.key,b)})}catch(_){}j&&k&&_utilities.Utilities.updateCache({cacheManager:this.trackingCache,hasJsCssVariables:n,visitorCode:b,experimentId:j,variationId:k}),d&&this.tracker.scheduleVisitor(b,this._isConsentProvided(b));return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${i})`,(0,_tsRes.Ok)({key:l,id:k,experimentId:j,variables:m})}}exports.KameleoonClient=KameleoonClient; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.KameleoonClient=void 0;var _tsRes=require("ts-res"),_cacheManager=require("./cacheManager"),_clientConfiguration=require("./clientConfiguration"),_clientSettings=require("./clientSettings"),_constants=require("./constants"),_kameleoonData=require("./kameleoonData"),_kameleoonError=require("./kameleoonError"),_requester=require("./requester"),_storage=require("./storage"),_types=require("./types"),_utilities=require("./utilities"),_variationConfiguration=require("./variationConfiguration"),_eventSource=require("./eventSource"),_visitorCodeManager=require("./visitorCodeManager"),_eventManager=require("./eventManager"),_tracking=require("./tracking"),_logging=require("./logging");function _defineProperty(a,b,c){return(b=_toPropertyKey(b))in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}function _toPropertyKey(a){var b=_toPrimitive(a,"string");return"symbol"==typeof b?b:b+""}function _toPrimitive(a,b){if("object"!=typeof a||!a)return a;var c=a[Symbol.toPrimitive];if(void 0!==c){var d=c.call(a,b||"default");if("object"!=typeof d)return d;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===b?String:Number)(a)}class KameleoonClient{constructor(a){var b;let{siteCode:c,dependencies:d,configuration:e,internalConfiguration:f}=a;_defineProperty(this,"variationConfiguration",void 0),_defineProperty(this,"clientConfiguration",void 0),_defineProperty(this,"dataManager",void 0),_defineProperty(this,"consentDataStorage",void 0),_defineProperty(this,"trackingCache",void 0),_defineProperty(this,"requester",void 0),_defineProperty(this,"externalPackageInfo",void 0),_defineProperty(this,"initialized",!1),_defineProperty(this,"visitorCodeManager",void 0),_defineProperty(this,"eventManager",void 0),_defineProperty(this,"tracker",void 0),_defineProperty(this,"environment",void 0);const g=null===f||void 0===f?void 0:f.externals.externalLogger;g&&_logging.KameleoonLogger.setLogger(g),_logging.KameleoonLogger.debug`CALL: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e}, internalConfiguration: ${f})`;const{settings:h}=new _clientSettings.ClientSettings(c,e);this.environment=h.environment;const{externalStorage:i,externalEventSource:j,externalPackageInfo:k,externalVisitorCodeManager:l,externalRequester:m,externalPRNG:n}=f.externals,{useAbortController:o}=f.settings,p=new _cacheManager.CacheManager(_constants.CACHE_CLEANUP_TIMEOUT),q=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ClientData),r=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TargetingData),s=new _storage.DataStorage(i,_storage.KameleoonStorageKey.DataInfo),t=new _storage.DataStorage(i,_storage.KameleoonStorageKey.VariationData),u=new _storage.DataStorage(i,_storage.KameleoonStorageKey.ConsentData),v=new _storage.DataStorage(i,_storage.KameleoonStorageKey.TrackingData),w=new _kameleoonData.DataManager({dataStorage:r,infoStorage:s,cleanupInterval:h.cleanupInterval}),x=null!==(b=null===d||void 0===d?void 0:d.urlProvider)&&void 0!==b?b:new _requester.UrlProvider;x.initialize({domain:h.networkDomain,siteCode:h.siteCode,packageInfo:k,environment:h.environment});const y=new _requester.Requester({urlProvider:x,useAbortController:o,packageInfo:k,externalRequester:m,requestTimeout:h.requestTimeout,trackRetryDelay:h.trackRetryDelay}),z=new _eventSource.KameleoonEventSource({siteCode:h.siteCode,externalEventSource:j,urlProvider:x}),A=new _eventManager.EventManager,B=new _clientConfiguration.ClientConfiguration({urlProvider:x,storage:q,updateInterval:h.updateInterval,dataManager:w,eventManager:A,requester:y,eventSource:z,externalVisitorCodeManager:l}),C=new _variationConfiguration.VariationConfiguration(t,l),D=new _tracking.Tracker({dataManager:w,trackingStorage:v,variationConfiguration:C,requester:y,trackingInterval:h.trackingInterval,prng:n});this.tracker=D,this.variationConfiguration=C,this.requester=y,this.trackingCache=p,this.dataManager=w,this.clientConfiguration=B,this.externalPackageInfo=k,this.consentDataStorage=u,this.visitorCodeManager=l,this.eventManager=A,_logging.KameleoonLogger.debug`RETURN: new KameleoonClient(siteCode: ${c}, dependencies: ${d}, configuration: ${e}, internalConfiguration: ${f})`}async initialize(){_logging.KameleoonLogger.info("CALL: KameleoonClient.initialize()");const a=await this.clientConfiguration.initialize();return a.throw(),this.initialized=!0,_logging.KameleoonLogger.info`RETURN: KameleoonClient.initialize() -> (result: ${a.ok})`,a.ok}addData(a){for(var b=arguments.length,c=Array(1<b?b-1:0),d=1;d<b;d++)c[d-1]=arguments[d];if(_logging.KameleoonLogger.info`CALL: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`,this.visitorCodeManager.validateVisitorCode(a).throw(),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.dataManager.storeData(a,...c).throw(),_logging.KameleoonLogger.info`RETURN: KameleoonClient.addData(visitorCode: ${a}, data: ${c})`}async getRemoteVisitorData(a){let{visitorCode:b,shouldAddData:c=!0,filters:d=_constants.DEFAULT_VISITOR_DATA_FILTERS}=a;if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.visitorCodeManager.validateVisitorCode(b).throw();const{previousVisitAmount:e}=d;if("number"==typeof e&&(1>e||25<e))throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.VisitAmount);const f=this.dataManager.isUniqueIdentifier(b),g=await this.requester.getVisitorData({visitorCode:b,filters:d,isMappingIdentifier:f}),h=g.throw(),{visitorData:i,storageVisitorData:j,visitsData:k}=_utilities.Parser.parseVisitorData({data:h,filters:d,visitorCode:b,dataManager:this.dataManager,variationConfiguration:this.variationConfiguration});for(const d of j)(c||this.dataManager.isPersistentCustomData(d))&&this.addData(b,d);return k&&c&&this.addData(b,k),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteVisitorData(visitorCode: ${b}, shouldAddData: ${c}, filters: ${d}) -> (visitorData: ${i})`,i}trackConversion(a){let{visitorCode:b,goalId:c,revenue:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`,this.addData(b,new _kameleoonData.Conversion({goalId:c,revenue:d})),this.flush(b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.trackConversion(visitorCode: ${b}, goalId: ${c}, revenue: ${d})`}flush(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.flush(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);if("string"==typeof a)return this.visitorCodeManager.validateVisitorCode(a).throw(),this.tracker.scheduleVisitor(a,this._isConsentProvided(a)),void _logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`;for(const b of this.dataManager.unsentDataVisitors)this.tracker.scheduleVisitor(b,this._isConsentProvided(b));_logging.KameleoonLogger.info`RETURN: KameleoonClient.flush(visitorCode: ${a})`}getFeatureFlags(){if(_logging.KameleoonLogger.info("CALL: KameleoonClient.getFeatureFlags()"),!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const a=this.clientConfiguration.featureFlags,b=[];return a.forEach(a=>{let{id:c,featureKey:d}=a;b.push({id:c,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlags() -> (FeatureFlags: ${b})`,b}getVisitorFeatureFlags(a){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const b=this._getActiveFeatureVariations(a),c=[];return b.forEach(a=>{let{featureFlagId:b,featureKey:d}=a;c.push({id:b,key:d})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getVariation(a){let{visitorCode:b,featureKey:c,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`;const e=this._getFeatureVariation({visitorCode:b,featureKey:c,track:d}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${e})`,e}getVariations(a){let{visitorCode:b,onlyActive:c=!1,track:d=!0}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d})`;const e=new Map,f=this.clientConfiguration.featureFlags;for(const g of f.values()){const a=this._getFeatureVariation({visitorCode:b,featureKey:g.featureKey,track:d});a.ok&&(!c||a.data.key!==_constants.OFF_VARIATION_KEY)&&e.set(g.featureKey,a.data)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVariations(visitorCode: ${b}, onlyActive: ${c}, track: ${d}) -> (variations: ${e})`,e}getActiveFeatureFlags(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a})`;const b=this._getActiveFeatureVariations(a),c=new Map;return b.forEach(b=>{const{variationKey:d,featureKey:e,variationId:f,experimentId:g}=b;let h=[];null!==f&&null!==g&&(h=this._getFeatureVariables({visitorCode:a,featureKey:e,variationKey:d}));const i=h.map(a=>_utilities.Parser.parseFeatureVariable(a).throw());c.set(b.featureKey,{key:d,id:f,experimentId:g,variables:i})}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getActiveFeatureFlags(visitorCode: ${a}) -> (FeatureFlags: ${c})`,c}getFeatureFlagVariationKey(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw();return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariationKey(visitorCode: ${a}, featureKey: ${b}) -> (variationKey: ${c})`,c}getFeatureFlagVariable(a){let{visitorCode:b,featureKey:c,variableKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const{key:e}=this._getFeatureVariation({visitorCode:b,featureKey:c,track:!0}).throw(),f=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:e}),g=f.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariableNotFound,d,b);const{type:h,value:i}=_utilities.Parser.parseFeatureVariable(g).throw(),j={type:h,value:i};return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariable(visitorCode: ${b}, featureKey: ${c}, variableKey: ${d}) -> (FeatureFlagVariable: ${j})`,j}getFeatureFlagVariables(a,b){_logging.KameleoonLogger.info`CALL: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b})`,this.visitorCodeManager.validateVisitorCode(a).throw();const{key:c}=this._getFeatureVariation({visitorCode:a,featureKey:b,track:!0}).throw(),d=this._getFeatureVariables({visitorCode:a,featureKey:b,variationKey:c}),e=[];for(const c of d){const a=_utilities.Parser.parseFeatureVariable(c).throw();e.push(a)}return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getFeatureFlagVariables(visitorCode: ${a}, featureKey: ${b}) -> (FeatureFlagVariables: ${e})`,e}isFeatureFlagActive(a,b){let[c,d,e]=["","",!0];"string"==typeof a?(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d})`,c=a,d=b):(_logging.KameleoonLogger.info`CALL: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track ${e})`,[c,d]=[a.visitorCode,a.featureKey],a.track!==void 0&&(e=a.track)),this.visitorCodeManager.validateVisitorCode(c).throw();try{const{key:a}=this._getFeatureVariation({visitorCode:c,featureKey:d,track:e}).throw(),b=a!==_constants.OFF_VARIATION_KEY;return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: ${b})`,b}catch(a){if(a instanceof _kameleoonError.KameleoonError&&a.type===_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.isFeatureFlagActive(visitorCode: ${c}, featureKey: ${d}, track: ${e}) -> (isActive: false)`,!1;throw a}}async getRemoteData(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getRemoteData(key: ${a})`;const b=await this.requester.getRemoteData(a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getRemoteData(key: ${a}) -> (remoteData: ${b})`,b.throw()}async getVisitorWarehouseAudience(a){let{visitorCode:b,customDataIndex:c,warehouseKey:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw();const e=await this.getRemoteData(d||b);if(!e)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.RemoteData,`No data found for ${d||b} key`);if(!e[_constants.WAREHOUSE_AUDIENCE_KEY]||0===Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]).length)return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: null)`,null;const f=new _kameleoonData.CustomData(c,...Object.keys(e[_constants.WAREHOUSE_AUDIENCE_KEY]));return this.dataManager.storeData(b,f),_logging.KameleoonLogger.info`RETURN: KameleoonClient.getVisitorWarehouseAudience(visitorCode: ${b}, customDataIndex: ${c}, warehouseKey: ${d}) -> (customData: ${f})`,f}onEvent(a,b){if(_logging.KameleoonLogger.info`CALL: KameleoonClient.onEvent(event: ${a}, callback)`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);this.eventManager.addEventHandler(a,b),_logging.KameleoonLogger.info`RETURN: KameleoonClient.onEvent(event: ${a}, callback)`}getEngineTrackingCode(a){_logging.KameleoonLogger.info`CALL: KameleoonClient.getEngineTrackingCode(visitorCode: ${a})`,this.visitorCodeManager.validateVisitorCode(a).throw();const b=_utilities.Utilities.getTrackingCode(this.trackingCache,a);return _logging.KameleoonLogger.info`RETURN: KameleoonClient.getEngineTrackingCode(visitorCode: ${a}) -> (trackingCode: ${b})`,b}isInitialized(){return _logging.KameleoonLogger.debug`CALL/RETURN: KameleoonClient.isInitialized() -> (initialized: ${this.initialized})`,this.initialized}setLogLevel(a){_logging.KameleoonLogger.setLogLevel(a)}setUserConsent(a){let{visitorCode:b,consent:c,setData:d}=a;_logging.KameleoonLogger.info`CALL: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`,this.visitorCodeManager.validateVisitorCode(b).throw(),this.consentDataStorage.update({[b]:c}),c?d({visitorCode:b,key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.DEFAULT_MAX_AGE,path:_visitorCodeManager.PATH}):this.visitorCodeManager.consentRequired&&d({visitorCode:"",key:_storage.KameleoonStorageKey.VisitorCode,maxAge:_visitorCodeManager.ZERO_MAX_AGE,path:_visitorCodeManager.PATH}),_logging.KameleoonLogger.info`RETURN: KameleoonClient.setUserConsent(visitorCode: ${b}, consent: ${c}, setData: ${d})`}_isConsentProvided(a){_logging.KameleoonLogger.debug`CALL: KameleoonClient._isConsentProvided(visitorCode: ${a})`;const{isConsentRequired:b}=this.clientConfiguration,c=this.consentDataStorage.read(),d=!b||c.ok&&c.data[a];return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._isConsentProvided(visitorCode: ${a}) -> (isConsentProvided: ${d})`,d}_getFeatureVariables(a){let{visitorCode:b,featureKey:c,variationKey:d}=a;_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d})`;const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c);const g=f.variations.find(a=>a.key===d);if(!g)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagVariationNotFound,d,b);return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariables(visitorCode: ${b}, featureKey: ${c}, variationKey: ${d}) -> (variables: ${g.variables})`,g.variables}_getActiveFeatureVariations(a){if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a})`,!this.initialized)throw new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization);const b=this.clientConfiguration.featureFlags,c=[],d=this.dataManager.getVisitorData(a),e=this.dataManager.getVisitorIdentifier(a);for(const f of b.values()){if(!f.environmentEnabled)continue;const b=this.variationConfiguration.getVariation({visitorCode:a,visitorIdentifier:e,targetingData:d,featureFlag:f,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:f.featureKey,variation:{key:b.variationKey,experimentId:b.experimentId,id:b.variationId}}),b.variationKey!==_constants.OFF_VARIATION_KEY&&c.push(b)}return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getActiveFeatureVariations(visitorCode: ${a}) -> (activeVariations: ${c})`,c}_getFeatureVariation(a){let{visitorCode:b,featureKey:c,track:d}=a;if(_logging.KameleoonLogger.debug`CALL: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d})`,!this.initialized)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.Initialization));const e=this.clientConfiguration.featureFlags,f=e.get(c);if(!f)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagConfigurationNotFound,c));if(!f.environmentEnabled)return(0,_tsRes.Err)(new _kameleoonError.KameleoonError(_kameleoonError.KameleoonException.FeatureFlagEnvironmentDisabled,f.featureKey,this.environment));const g=this.dataManager.getVisitorData(b),h=this.dataManager.getVisitorIdentifier(b),i=this.variationConfiguration.getVariation({visitorCode:b,visitorIdentifier:h,featureFlag:f,track:d,withAssignment:!0,targetingData:g,clientConfiguration:this.clientConfiguration,dataManager:this.dataManager,packageInfo:this.externalPackageInfo}).throw();this.eventManager.fireEvent(_eventManager.EventType.Evaluation,{featureKey:c,variation:{key:i.variationKey,experimentId:i.experimentId,id:i.variationId}});const{experimentId:j,variationId:k,variationKey:l}=i;let m=new Map,n=!1;try{const a=this._getFeatureVariables({visitorCode:b,featureKey:c,variationKey:l});a.forEach(a=>{n||a.type!==_types.VariableType.JS&&a.type!==_types.VariableType.CSS||(n=!0);const b=_utilities.Parser.parseFeatureVariable(a).throw();m.set(a.key,b)})}catch(_){}j&&"number"==typeof k&&_utilities.Utilities.updateCache({cacheManager:this.trackingCache,hasJsCssVariables:n,visitorCode:b,experimentId:j,variationId:k}),d&&this.tracker.scheduleVisitor(b,this._isConsentProvided(b));return _logging.KameleoonLogger.debug`RETURN: KameleoonClient._getFeatureVariation(visitorCode: ${b}, featureKey: ${c}, track: ${d}) -> (variation: ${i})`,(0,_tsRes.Ok)({key:l,id:k,experimentId:j,variables:m})}}exports.KameleoonClient=KameleoonClient; |
{ | ||
"name": "@kameleoon/javascript-sdk-core", | ||
"version": "5.1.2", | ||
"version": "5.1.3", | ||
"description": "Kameleoon JS SDK Core", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
422481