@uniformdev/optimize-tracker
Advanced tools
Comparing version 2.0.1-alpha.34 to 2.0.1-alpha.35
export * from './tracker'; | ||
export * from './intentManifest/addLocalIntentManifestPlugin'; | ||
export { personalizeList } from './personalization/personalizeList'; | ||
@@ -4,0 +3,0 @@ export * from './logging/consoleLogger'; |
@@ -15,3 +15,2 @@ "use strict"; | ||
__exportStar(require("./tracker"), exports); | ||
__exportStar(require("./intentManifest/addLocalIntentManifestPlugin"), exports); | ||
var personalizeList_1 = require("./personalization/personalizeList"); | ||
@@ -18,0 +17,0 @@ Object.defineProperty(exports, "personalizeList", { enumerable: true, get: function () { return personalizeList_1.personalizeList; } }); |
import { TrackerPlugin, LogMessageFunction, TrackFunction, TrackerPlugins, InitializeFunction, ScoringChangeListener } from '@uniformdev/optimize-tracker-common'; | ||
import { DeliveryIntent } from '@uniformdev/optimize-common'; | ||
export declare class OptimizePluginLoader implements TrackerPlugins { | ||
@@ -9,3 +8,2 @@ private readonly _pluginMap; | ||
initialize(callback: (func: InitializeFunction) => void | Promise<void>): Promise<void>; | ||
loadIntents(): Promise<DeliveryIntent[]>; | ||
scoringChange(callback: (func: ScoringChangeListener) => void): void; | ||
@@ -12,0 +10,0 @@ private mapPlugins; |
@@ -68,24 +68,2 @@ "use strict"; | ||
}; | ||
OptimizePluginLoader.prototype.loadIntents = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var functions, promises, resolved, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
functions = this.find('onLoadIntents'); | ||
promises = functions.map(function (func) { return func(); }); | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 1: | ||
resolved = _a.sent(); | ||
response = []; | ||
resolved.forEach(function (value) { | ||
if (value) { | ||
response.push.apply(response, value); | ||
} | ||
}); | ||
return [2 /*return*/, response]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizePluginLoader.prototype.scoringChange = function (callback) { | ||
@@ -92,0 +70,0 @@ var functions = this.find('onScoringChange'); |
import { SignalEvaluator, TrackerPlugin } from '@uniformdev/optimize-tracker-common'; | ||
import { StringMatch } from '@uniformdev/optimize-common'; | ||
export declare const doesValueMatchEventProperty: (stringValue?: string | undefined, stringMatch?: StringMatch | undefined) => boolean; | ||
export declare const doesValueMatchEventProperty: (stringValue: string | undefined, stringMatch: StringMatch | undefined) => boolean; | ||
export declare const eventSignalEvaluator: SignalEvaluator<undefined>; | ||
export declare const addEventSignalPlugin: () => TrackerPlugin; |
@@ -6,3 +6,3 @@ "use strict"; | ||
var doesValueMatchEventProperty = function (stringValue, stringMatch) { | ||
if (!stringMatch) { | ||
if (!stringMatch || !stringMatch.expr) { | ||
return true; | ||
@@ -9,0 +9,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Tracker, TrackerOptions, LogMessageOptions, ListenerFunction, AnalyticsEventPayload, TrackerRequestContext, EvaluatedSignal, ReevaluateSignalsResults, TrackerEvent, TrackerPushEventOptions, TrackerScope, TrackerTestCheckOptions, SignalEvaluatorResult } from '@uniformdev/optimize-tracker-common'; | ||
import { Tracker, TrackerOptions, LogMessageOptions, ListenerFunction, AnalyticsEventPayload, TrackerRequestContext, EvaluatedSignal, ReevaluateSignalsResults, TrackerEvent, TrackerPushEventOptions, TrackerScope, TrackerTestCheckOptions, SignalEvaluatorResult, TrackerAttribute } from '@uniformdev/optimize-tracker-common'; | ||
import { DeliveryIntent, IntentAxis, IntentVector, Scope, Signal } from '@uniformdev/optimize-common'; | ||
@@ -23,3 +23,4 @@ declare type ReevaluateSignalResult = { | ||
export declare class OptimizeTracker implements Tracker { | ||
private readonly _plugins; | ||
private readonly _intentManifest; | ||
private readonly _plugins?; | ||
private readonly _scoringStrategy; | ||
@@ -38,7 +39,9 @@ private readonly _decayStrategy?; | ||
private _initialized; | ||
private _previouslyBroadcastedScore; | ||
constructor(options: TrackerOptions); | ||
isIncludedInTest(options?: TrackerTestCheckOptions): boolean; | ||
private getScopeNames; | ||
removeIntent(intent?: string, scope?: Scope | string): Promise<boolean>; | ||
removeIntent(intent?: string | string[], options?: { | ||
scope?: Scope | string; | ||
quiet?: boolean; | ||
}): Promise<boolean>; | ||
getIntentStrength(scope?: Scope | string): Promise<IntentVector | undefined>; | ||
@@ -72,3 +75,2 @@ getIntentStrengthSingle(vector: string, scope?: Scope | string): Promise<IntentAxis | undefined>; | ||
private loadSignalEvaluators; | ||
private loadIntents; | ||
private addIntents; | ||
@@ -86,3 +88,16 @@ private readScoring; | ||
addEvent(event: TrackerEvent, options?: TrackerPushEventOptions): Promise<void>; | ||
private isKnownAttribute; | ||
removeAttribute(attributeName: string, options?: { | ||
scope?: Scope; | ||
quiet?: boolean; | ||
}): Promise<void>; | ||
addAttribute(attribute: TrackerAttribute | TrackerAttribute[], options?: { | ||
quiet?: boolean; | ||
scope?: Scope; | ||
clearValues?: boolean; | ||
}): Promise<void>; | ||
getAttributeValues(attributeName: string, options?: { | ||
scope?: Scope; | ||
}): Promise<Record<string, number> | undefined>; | ||
} | ||
export {}; |
@@ -61,2 +61,3 @@ "use strict"; | ||
var cumulativeScoringStrategy_1 = require("./scoring/cumulativeScoringStrategy"); | ||
var utils_1 = require("./utils"); | ||
var OptimizeTracker = /** @class */ (function () { | ||
@@ -74,3 +75,6 @@ function OptimizeTracker(options) { | ||
this._initialized = false; | ||
this._previouslyBroadcastedScore = undefined; | ||
this._intentManifest = options.intentManifest; | ||
if (options.intentManifest) { | ||
this.addIntents(options.intentManifest.site.intents); | ||
} | ||
this._plugins = options.plugins; | ||
@@ -110,9 +114,10 @@ this._storage = options.storage; | ||
}; | ||
OptimizeTracker.prototype.removeIntent = function (intent, scope) { | ||
OptimizeTracker.prototype.removeIntent = function (intent, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, wasSomethingRemovedFromAnyScope, _loop_1, this_1, _a, _b, _i, currentScope; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var scopes, intentsToRemove, wasSomethingRemovedFromAnyScope, _loop_1, this_1, _i, scopes_1, currentScope; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
scopes = this.getScopeNames(scope); | ||
scopes = this.getScopeNames(options === null || options === void 0 ? void 0 : options.scope); | ||
intentsToRemove = typeof intent === 'string' ? [intent] : intent; | ||
wasSomethingRemovedFromAnyScope = false; | ||
@@ -130,3 +135,3 @@ _loop_1 = function (currentScope) { | ||
Object.keys(scoring.values).map(function (key) { | ||
if (intent && key !== intent) { | ||
if (intent && (intentsToRemove === null || intentsToRemove === void 0 ? void 0 : intentsToRemove.indexOf(key)) === -1) { | ||
updatedScoring_1[key] = scoring.values[key]; | ||
@@ -140,3 +145,3 @@ } | ||
if (somethingWasRemoved_1) { | ||
this_1.writeScoring(currentScope, __assign(__assign({}, scoring), { values: updatedScoring_1 })); | ||
this_1.writeScoring(currentScope, __assign(__assign({}, scoring), { values: updatedScoring_1 }), options === null || options === void 0 ? void 0 : options.quiet); | ||
} | ||
@@ -149,14 +154,11 @@ } | ||
this_1 = this; | ||
_a = []; | ||
for (_b in scopes) | ||
_a.push(_b); | ||
_i = 0; | ||
_c.label = 1; | ||
_i = 0, scopes_1 = scopes; | ||
_a.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3 /*break*/, 4]; | ||
currentScope = _a[_i]; | ||
if (!(_i < scopes_1.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_1[_i]; | ||
return [5 /*yield**/, _loop_1(currentScope)]; | ||
case 2: | ||
_c.sent(); | ||
_c.label = 3; | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
@@ -172,3 +174,3 @@ _i++; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, scoring, _i, scopes_1, currentScope, _a, _b, combined; | ||
var scopes, scoring, _i, scopes_2, currentScope, _a, _b, combined; | ||
return __generator(this, function (_c) { | ||
@@ -179,7 +181,7 @@ switch (_c.label) { | ||
scoring = {}; | ||
_i = 0, scopes_1 = scopes; | ||
_i = 0, scopes_2 = scopes; | ||
_c.label = 1; | ||
case 1: | ||
if (!(_i < scopes_1.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_1[_i]; | ||
if (!(_i < scopes_2.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_2[_i]; | ||
_a = scoring; | ||
@@ -229,7 +231,9 @@ _b = currentScope; | ||
OptimizeTracker.prototype.log = function (data) { | ||
this._plugins.log(function (log) { return log(data); }); | ||
var _a; | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.log(function (log) { return log(data); }); | ||
}; | ||
OptimizeTracker.prototype.trackPersonalization = function (name, payload) { | ||
var _a; | ||
if (name) { | ||
this._plugins.track(function (track) { return track(name, payload); }); | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.track(function (track) { return track(name, payload); }); | ||
} | ||
@@ -658,5 +662,2 @@ }; | ||
_a.sent(); | ||
return [4 /*yield*/, this.loadIntents()]; | ||
case 3: | ||
_a.sent(); | ||
this._signalEvaluators = this.loadSignalEvaluators(); | ||
@@ -712,3 +713,4 @@ this._initialized = true; | ||
var _this = this; | ||
var signals = this._plugins.find('signalEvaluatorMapping'); | ||
var _a; | ||
var signals = ((_a = this._plugins) === null || _a === void 0 ? void 0 : _a.find('signalEvaluatorMapping')) || []; | ||
return signals.reduce(function (result, plugin) { | ||
@@ -728,16 +730,2 @@ var pluginEvaluatorMappings = plugin(); | ||
}; | ||
OptimizeTracker.prototype.loadIntents = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var intents; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._plugins.loadIntents()]; | ||
case 1: | ||
intents = _a.sent(); | ||
this.addIntents(intents); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.addIntents = function (intents) { | ||
@@ -789,4 +777,4 @@ var _a; | ||
var scopes = Object.keys(this._scopeStrategies); | ||
for (var _i = 0, scopes_2 = scopes; _i < scopes_2.length; _i++) { | ||
var key = scopes_2[_i]; | ||
for (var _i = 0, scopes_3 = scopes; _i < scopes_3.length; _i++) { | ||
var key = scopes_3[_i]; | ||
var strategy = this._scopeStrategies[key]; | ||
@@ -896,4 +884,7 @@ if (strategy) { | ||
}; | ||
OptimizeTracker.prototype.writeScoring = function (scope, scoring) { | ||
this._plugins.scoringChange(function (func) { return func(scoring === null || scoring === void 0 ? void 0 : scoring.values); }); | ||
OptimizeTracker.prototype.writeScoring = function (scope, scoring, quiet) { | ||
var _a; | ||
if (!quiet) { | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.scoringChange(function (func) { return func(scoring === null || scoring === void 0 ? void 0 : scoring.values); }); | ||
} | ||
this.log(__assign({ level: 'verbose', message: 'Scoring has been updated' }, scoring.values)); | ||
@@ -916,3 +907,2 @@ return this.writeToStorage(scope, scoring); // yolo. swag. git push -f origin master | ||
value = _a; | ||
this._previouslyBroadcastedScore = scoring; | ||
this._listeners.forEach(function (listener) { return listener(value || null); }); | ||
@@ -926,3 +916,3 @@ return [2 /*return*/]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, _i, scopes_3, currentScope; | ||
var scopes, _i, scopes_4, currentScope; | ||
return __generator(this, function (_a) { | ||
@@ -937,7 +927,7 @@ switch (_a.label) { | ||
scopes = this.getScopeNames(); | ||
_i = 0, scopes_3 = scopes; | ||
_i = 0, scopes_4 = scopes; | ||
_a.label = 3; | ||
case 3: | ||
if (!(_i < scopes_3.length)) return [3 /*break*/, 6]; | ||
currentScope = scopes_3[_i]; | ||
if (!(_i < scopes_4.length)) return [3 /*break*/, 6]; | ||
currentScope = scopes_4[_i]; | ||
return [4 /*yield*/, this._storage.scoring.delete(currentScope)]; | ||
@@ -975,4 +965,113 @@ case 4: | ||
}; | ||
OptimizeTracker.prototype.isKnownAttribute = function (attributeName) { | ||
var _a, _b; | ||
var manifestAttribute = (_b = (_a = this._intentManifest) === null || _a === void 0 ? void 0 : _a.site.attributes) === null || _b === void 0 ? void 0 : _b.find(function (attr) { return attr.name === attributeName; }); | ||
return !!manifestAttribute; | ||
}; | ||
OptimizeTracker.prototype.removeAttribute = function (attributeName, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var targetScope, current, keysToRemove; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
targetScope = (options === null || options === void 0 ? void 0 : options.scope) || optimize_common_1.Scope.Visitor; | ||
return [4 /*yield*/, this.getIntentStrength(targetScope)]; | ||
case 1: | ||
current = (_a.sent()) || {}; | ||
keysToRemove = Object.keys(current || {}).filter(function (key) { | ||
var type = utils_1.getAttributeValue(attributeName, key); | ||
if (type) { | ||
return key; | ||
} | ||
}, {}); | ||
return [4 /*yield*/, this.removeIntent(keysToRemove, { | ||
scope: targetScope, | ||
})]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.addAttribute = function (attribute, options) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var targetScope, addedAttributes, valueToAdd, attributesToClear, _i, addedAttributes_1, current, isKnown, name_1, existingStr, _b, _c, attributeName, currentValue; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
targetScope = (options === null || options === void 0 ? void 0 : options.scope) || optimize_common_1.Scope.Visitor; | ||
addedAttributes = Array.isArray(attribute) ? attribute : [attribute]; | ||
valueToAdd = {}; | ||
attributesToClear = {}; | ||
for (_i = 0, addedAttributes_1 = addedAttributes; _i < addedAttributes_1.length; _i++) { | ||
current = addedAttributes_1[_i]; | ||
isKnown = this.isKnownAttribute(current.name); | ||
if (isKnown) { | ||
name_1 = utils_1.calculateAttributeIntentName(current.name, current.value); | ||
if (options === null || options === void 0 ? void 0 : options.clearValues) { | ||
attributesToClear[current.name] = true; | ||
} | ||
existingStr = Number(((_a = valueToAdd[name_1]) === null || _a === void 0 ? void 0 : _a.str) || 0); | ||
valueToAdd[name_1] = { | ||
str: existingStr + (current.str || optimize_common_1.IntentTagStrength.Normal), | ||
}; | ||
} | ||
} | ||
_b = 0, _c = Object.keys(attributesToClear); | ||
_d.label = 1; | ||
case 1: | ||
if (!(_b < _c.length)) return [3 /*break*/, 4]; | ||
attributeName = _c[_b]; | ||
return [4 /*yield*/, this.removeAttribute(attributeName, { scope: targetScope, quiet: true })]; | ||
case 2: | ||
_d.sent(); | ||
_d.label = 3; | ||
case 3: | ||
_b++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [4 /*yield*/, this.readScoring(targetScope)]; | ||
case 5: | ||
currentValue = _d.sent(); | ||
return [4 /*yield*/, this.applyScoring(targetScope, currentValue, valueToAdd, addedAttributes.length)]; | ||
case 6: | ||
_d.sent(); | ||
if (!!(options === null || options === void 0 ? void 0 : options.quiet)) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, this.reevaluateSignals()]; | ||
case 7: | ||
_d.sent(); | ||
_d.label = 8; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.getAttributeValues = function (attributeName, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var isKnown, current, values; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
isKnown = this.isKnownAttribute(attributeName); | ||
if (!isKnown) { | ||
return [2 /*return*/, undefined]; | ||
} | ||
return [4 /*yield*/, this.getIntentStrength(options === null || options === void 0 ? void 0 : options.scope)]; | ||
case 1: | ||
current = (_a.sent()) || {}; | ||
values = Object.keys(current || {}).reduce(function (previous, key) { | ||
var type = utils_1.getAttributeValue(attributeName, key); | ||
if (type && current[key].str) { | ||
previous[type] = Number(current[key].str || 0); | ||
} | ||
return previous; | ||
}, {}); | ||
return [2 /*return*/, values]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return OptimizeTracker; | ||
}()); | ||
exports.OptimizeTracker = OptimizeTracker; |
@@ -9,1 +9,4 @@ import { TrackerLogLevels } from '@uniformdev/optimize-tracker-common'; | ||
export declare const isLogLevelUnderThreshold: (threshold: TrackerLogLevels, level: TrackerLogLevels) => boolean; | ||
export declare const attributeDivider = ">"; | ||
export declare const calculateAttributeIntentName: (attributeType: string, attributeValue: string) => string; | ||
export declare const getAttributeValue: (attributeType: string, storedName: string) => string | undefined; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isLogLevelUnderThreshold = exports.logLevelMap = void 0; | ||
exports.getAttributeValue = exports.calculateAttributeIntentName = exports.attributeDivider = exports.isLogLevelUnderThreshold = exports.logLevelMap = void 0; | ||
exports.logLevelMap = { | ||
@@ -19,1 +19,16 @@ error: 3, | ||
exports.isLogLevelUnderThreshold = isLogLevelUnderThreshold; | ||
exports.attributeDivider = '>'; | ||
var calculateAttributeIntentName = function (attributeType, attributeValue) { | ||
if (attributeType.includes(exports.attributeDivider) || attributeValue.includes(exports.attributeDivider)) { | ||
throw "\"" + exports.attributeDivider + "\" is a reserved attribute character"; | ||
} | ||
return "" + attributeType + exports.attributeDivider + attributeValue; | ||
}; | ||
exports.calculateAttributeIntentName = calculateAttributeIntentName; | ||
var getAttributeValue = function (attributeType, storedName) { | ||
var _a = storedName.split(exports.attributeDivider), type = _a[0], value = _a[1]; | ||
if (type === attributeType) { | ||
return value; | ||
} | ||
}; | ||
exports.getAttributeValue = getAttributeValue; |
export * from './tracker'; | ||
export * from './intentManifest/addLocalIntentManifestPlugin'; | ||
export { personalizeList } from './personalization/personalizeList'; | ||
@@ -4,0 +3,0 @@ export * from './logging/consoleLogger'; |
export * from './tracker'; | ||
export * from './intentManifest/addLocalIntentManifestPlugin'; | ||
export { personalizeList } from './personalization/personalizeList'; | ||
@@ -4,0 +3,0 @@ export * from './logging/consoleLogger'; |
import { TrackerPlugin, LogMessageFunction, TrackFunction, TrackerPlugins, InitializeFunction, ScoringChangeListener } from '@uniformdev/optimize-tracker-common'; | ||
import { DeliveryIntent } from '@uniformdev/optimize-common'; | ||
export declare class OptimizePluginLoader implements TrackerPlugins { | ||
@@ -9,3 +8,2 @@ private readonly _pluginMap; | ||
initialize(callback: (func: InitializeFunction) => void | Promise<void>): Promise<void>; | ||
loadIntents(): Promise<DeliveryIntent[]>; | ||
scoringChange(callback: (func: ScoringChangeListener) => void): void; | ||
@@ -12,0 +10,0 @@ private mapPlugins; |
@@ -65,24 +65,2 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
OptimizePluginLoader.prototype.loadIntents = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var functions, promises, resolved, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
functions = this.find('onLoadIntents'); | ||
promises = functions.map(function (func) { return func(); }); | ||
return [4 /*yield*/, Promise.all(promises)]; | ||
case 1: | ||
resolved = _a.sent(); | ||
response = []; | ||
resolved.forEach(function (value) { | ||
if (value) { | ||
response.push.apply(response, value); | ||
} | ||
}); | ||
return [2 /*return*/, response]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizePluginLoader.prototype.scoringChange = function (callback) { | ||
@@ -89,0 +67,0 @@ var functions = this.find('onScoringChange'); |
import { SignalEvaluator, TrackerPlugin } from '@uniformdev/optimize-tracker-common'; | ||
import { StringMatch } from '@uniformdev/optimize-common'; | ||
export declare const doesValueMatchEventProperty: (stringValue?: string | undefined, stringMatch?: StringMatch | undefined) => boolean; | ||
export declare const doesValueMatchEventProperty: (stringValue: string | undefined, stringMatch: StringMatch | undefined) => boolean; | ||
export declare const eventSignalEvaluator: SignalEvaluator<undefined>; | ||
export declare const addEventSignalPlugin: () => TrackerPlugin; |
import { isEventSignal, isStringMatch, SignalType } from '@uniformdev/optimize-common'; | ||
export var doesValueMatchEventProperty = function (stringValue, stringMatch) { | ||
if (!stringMatch) { | ||
if (!stringMatch || !stringMatch.expr) { | ||
return true; | ||
@@ -5,0 +5,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { Tracker, TrackerOptions, LogMessageOptions, ListenerFunction, AnalyticsEventPayload, TrackerRequestContext, EvaluatedSignal, ReevaluateSignalsResults, TrackerEvent, TrackerPushEventOptions, TrackerScope, TrackerTestCheckOptions, SignalEvaluatorResult } from '@uniformdev/optimize-tracker-common'; | ||
import { Tracker, TrackerOptions, LogMessageOptions, ListenerFunction, AnalyticsEventPayload, TrackerRequestContext, EvaluatedSignal, ReevaluateSignalsResults, TrackerEvent, TrackerPushEventOptions, TrackerScope, TrackerTestCheckOptions, SignalEvaluatorResult, TrackerAttribute } from '@uniformdev/optimize-tracker-common'; | ||
import { DeliveryIntent, IntentAxis, IntentVector, Scope, Signal } from '@uniformdev/optimize-common'; | ||
@@ -23,3 +23,4 @@ declare type ReevaluateSignalResult = { | ||
export declare class OptimizeTracker implements Tracker { | ||
private readonly _plugins; | ||
private readonly _intentManifest; | ||
private readonly _plugins?; | ||
private readonly _scoringStrategy; | ||
@@ -38,7 +39,9 @@ private readonly _decayStrategy?; | ||
private _initialized; | ||
private _previouslyBroadcastedScore; | ||
constructor(options: TrackerOptions); | ||
isIncludedInTest(options?: TrackerTestCheckOptions): boolean; | ||
private getScopeNames; | ||
removeIntent(intent?: string, scope?: Scope | string): Promise<boolean>; | ||
removeIntent(intent?: string | string[], options?: { | ||
scope?: Scope | string; | ||
quiet?: boolean; | ||
}): Promise<boolean>; | ||
getIntentStrength(scope?: Scope | string): Promise<IntentVector | undefined>; | ||
@@ -72,3 +75,2 @@ getIntentStrengthSingle(vector: string, scope?: Scope | string): Promise<IntentAxis | undefined>; | ||
private loadSignalEvaluators; | ||
private loadIntents; | ||
private addIntents; | ||
@@ -86,3 +88,16 @@ private readScoring; | ||
addEvent(event: TrackerEvent, options?: TrackerPushEventOptions): Promise<void>; | ||
private isKnownAttribute; | ||
removeAttribute(attributeName: string, options?: { | ||
scope?: Scope; | ||
quiet?: boolean; | ||
}): Promise<void>; | ||
addAttribute(attribute: TrackerAttribute | TrackerAttribute[], options?: { | ||
quiet?: boolean; | ||
scope?: Scope; | ||
clearValues?: boolean; | ||
}): Promise<void>; | ||
getAttributeValues(attributeName: string, options?: { | ||
scope?: Scope; | ||
}): Promise<Record<string, number> | undefined>; | ||
} | ||
export {}; |
@@ -58,2 +58,3 @@ var __assign = (this && this.__assign) || function () { | ||
import { cumulativeScoringStrategy } from './scoring/cumulativeScoringStrategy'; | ||
import { calculateAttributeIntentName, getAttributeValue } from './utils'; | ||
var OptimizeTracker = /** @class */ (function () { | ||
@@ -71,3 +72,6 @@ function OptimizeTracker(options) { | ||
this._initialized = false; | ||
this._previouslyBroadcastedScore = undefined; | ||
this._intentManifest = options.intentManifest; | ||
if (options.intentManifest) { | ||
this.addIntents(options.intentManifest.site.intents); | ||
} | ||
this._plugins = options.plugins; | ||
@@ -107,9 +111,10 @@ this._storage = options.storage; | ||
}; | ||
OptimizeTracker.prototype.removeIntent = function (intent, scope) { | ||
OptimizeTracker.prototype.removeIntent = function (intent, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, wasSomethingRemovedFromAnyScope, _loop_1, this_1, _a, _b, _i, currentScope; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var scopes, intentsToRemove, wasSomethingRemovedFromAnyScope, _loop_1, this_1, _i, scopes_1, currentScope; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
scopes = this.getScopeNames(scope); | ||
scopes = this.getScopeNames(options === null || options === void 0 ? void 0 : options.scope); | ||
intentsToRemove = typeof intent === 'string' ? [intent] : intent; | ||
wasSomethingRemovedFromAnyScope = false; | ||
@@ -127,3 +132,3 @@ _loop_1 = function (currentScope) { | ||
Object.keys(scoring.values).map(function (key) { | ||
if (intent && key !== intent) { | ||
if (intent && (intentsToRemove === null || intentsToRemove === void 0 ? void 0 : intentsToRemove.indexOf(key)) === -1) { | ||
updatedScoring_1[key] = scoring.values[key]; | ||
@@ -137,3 +142,3 @@ } | ||
if (somethingWasRemoved_1) { | ||
this_1.writeScoring(currentScope, __assign(__assign({}, scoring), { values: updatedScoring_1 })); | ||
this_1.writeScoring(currentScope, __assign(__assign({}, scoring), { values: updatedScoring_1 }), options === null || options === void 0 ? void 0 : options.quiet); | ||
} | ||
@@ -146,14 +151,11 @@ } | ||
this_1 = this; | ||
_a = []; | ||
for (_b in scopes) | ||
_a.push(_b); | ||
_i = 0; | ||
_c.label = 1; | ||
_i = 0, scopes_1 = scopes; | ||
_a.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) return [3 /*break*/, 4]; | ||
currentScope = _a[_i]; | ||
if (!(_i < scopes_1.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_1[_i]; | ||
return [5 /*yield**/, _loop_1(currentScope)]; | ||
case 2: | ||
_c.sent(); | ||
_c.label = 3; | ||
_a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
@@ -169,3 +171,3 @@ _i++; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, scoring, _i, scopes_1, currentScope, _a, _b, combined; | ||
var scopes, scoring, _i, scopes_2, currentScope, _a, _b, combined; | ||
return __generator(this, function (_c) { | ||
@@ -176,7 +178,7 @@ switch (_c.label) { | ||
scoring = {}; | ||
_i = 0, scopes_1 = scopes; | ||
_i = 0, scopes_2 = scopes; | ||
_c.label = 1; | ||
case 1: | ||
if (!(_i < scopes_1.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_1[_i]; | ||
if (!(_i < scopes_2.length)) return [3 /*break*/, 4]; | ||
currentScope = scopes_2[_i]; | ||
_a = scoring; | ||
@@ -226,7 +228,9 @@ _b = currentScope; | ||
OptimizeTracker.prototype.log = function (data) { | ||
this._plugins.log(function (log) { return log(data); }); | ||
var _a; | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.log(function (log) { return log(data); }); | ||
}; | ||
OptimizeTracker.prototype.trackPersonalization = function (name, payload) { | ||
var _a; | ||
if (name) { | ||
this._plugins.track(function (track) { return track(name, payload); }); | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.track(function (track) { return track(name, payload); }); | ||
} | ||
@@ -655,5 +659,2 @@ }; | ||
_a.sent(); | ||
return [4 /*yield*/, this.loadIntents()]; | ||
case 3: | ||
_a.sent(); | ||
this._signalEvaluators = this.loadSignalEvaluators(); | ||
@@ -709,3 +710,4 @@ this._initialized = true; | ||
var _this = this; | ||
var signals = this._plugins.find('signalEvaluatorMapping'); | ||
var _a; | ||
var signals = ((_a = this._plugins) === null || _a === void 0 ? void 0 : _a.find('signalEvaluatorMapping')) || []; | ||
return signals.reduce(function (result, plugin) { | ||
@@ -725,16 +727,2 @@ var pluginEvaluatorMappings = plugin(); | ||
}; | ||
OptimizeTracker.prototype.loadIntents = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var intents; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this._plugins.loadIntents()]; | ||
case 1: | ||
intents = _a.sent(); | ||
this.addIntents(intents); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.addIntents = function (intents) { | ||
@@ -786,4 +774,4 @@ var _a; | ||
var scopes = Object.keys(this._scopeStrategies); | ||
for (var _i = 0, scopes_2 = scopes; _i < scopes_2.length; _i++) { | ||
var key = scopes_2[_i]; | ||
for (var _i = 0, scopes_3 = scopes; _i < scopes_3.length; _i++) { | ||
var key = scopes_3[_i]; | ||
var strategy = this._scopeStrategies[key]; | ||
@@ -893,4 +881,7 @@ if (strategy) { | ||
}; | ||
OptimizeTracker.prototype.writeScoring = function (scope, scoring) { | ||
this._plugins.scoringChange(function (func) { return func(scoring === null || scoring === void 0 ? void 0 : scoring.values); }); | ||
OptimizeTracker.prototype.writeScoring = function (scope, scoring, quiet) { | ||
var _a; | ||
if (!quiet) { | ||
(_a = this._plugins) === null || _a === void 0 ? void 0 : _a.scoringChange(function (func) { return func(scoring === null || scoring === void 0 ? void 0 : scoring.values); }); | ||
} | ||
this.log(__assign({ level: 'verbose', message: 'Scoring has been updated' }, scoring.values)); | ||
@@ -913,3 +904,2 @@ return this.writeToStorage(scope, scoring); // yolo. swag. git push -f origin master | ||
value = _a; | ||
this._previouslyBroadcastedScore = scoring; | ||
this._listeners.forEach(function (listener) { return listener(value || null); }); | ||
@@ -923,3 +913,3 @@ return [2 /*return*/]; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var scopes, _i, scopes_3, currentScope; | ||
var scopes, _i, scopes_4, currentScope; | ||
return __generator(this, function (_a) { | ||
@@ -934,7 +924,7 @@ switch (_a.label) { | ||
scopes = this.getScopeNames(); | ||
_i = 0, scopes_3 = scopes; | ||
_i = 0, scopes_4 = scopes; | ||
_a.label = 3; | ||
case 3: | ||
if (!(_i < scopes_3.length)) return [3 /*break*/, 6]; | ||
currentScope = scopes_3[_i]; | ||
if (!(_i < scopes_4.length)) return [3 /*break*/, 6]; | ||
currentScope = scopes_4[_i]; | ||
return [4 /*yield*/, this._storage.scoring.delete(currentScope)]; | ||
@@ -972,4 +962,113 @@ case 4: | ||
}; | ||
OptimizeTracker.prototype.isKnownAttribute = function (attributeName) { | ||
var _a, _b; | ||
var manifestAttribute = (_b = (_a = this._intentManifest) === null || _a === void 0 ? void 0 : _a.site.attributes) === null || _b === void 0 ? void 0 : _b.find(function (attr) { return attr.name === attributeName; }); | ||
return !!manifestAttribute; | ||
}; | ||
OptimizeTracker.prototype.removeAttribute = function (attributeName, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var targetScope, current, keysToRemove; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
targetScope = (options === null || options === void 0 ? void 0 : options.scope) || Scope.Visitor; | ||
return [4 /*yield*/, this.getIntentStrength(targetScope)]; | ||
case 1: | ||
current = (_a.sent()) || {}; | ||
keysToRemove = Object.keys(current || {}).filter(function (key) { | ||
var type = getAttributeValue(attributeName, key); | ||
if (type) { | ||
return key; | ||
} | ||
}, {}); | ||
return [4 /*yield*/, this.removeIntent(keysToRemove, { | ||
scope: targetScope, | ||
})]; | ||
case 2: | ||
_a.sent(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.addAttribute = function (attribute, options) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var targetScope, addedAttributes, valueToAdd, attributesToClear, _i, addedAttributes_1, current, isKnown, name_1, existingStr, _b, _c, attributeName, currentValue; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
targetScope = (options === null || options === void 0 ? void 0 : options.scope) || Scope.Visitor; | ||
addedAttributes = Array.isArray(attribute) ? attribute : [attribute]; | ||
valueToAdd = {}; | ||
attributesToClear = {}; | ||
for (_i = 0, addedAttributes_1 = addedAttributes; _i < addedAttributes_1.length; _i++) { | ||
current = addedAttributes_1[_i]; | ||
isKnown = this.isKnownAttribute(current.name); | ||
if (isKnown) { | ||
name_1 = calculateAttributeIntentName(current.name, current.value); | ||
if (options === null || options === void 0 ? void 0 : options.clearValues) { | ||
attributesToClear[current.name] = true; | ||
} | ||
existingStr = Number(((_a = valueToAdd[name_1]) === null || _a === void 0 ? void 0 : _a.str) || 0); | ||
valueToAdd[name_1] = { | ||
str: existingStr + (current.str || IntentTagStrength.Normal), | ||
}; | ||
} | ||
} | ||
_b = 0, _c = Object.keys(attributesToClear); | ||
_d.label = 1; | ||
case 1: | ||
if (!(_b < _c.length)) return [3 /*break*/, 4]; | ||
attributeName = _c[_b]; | ||
return [4 /*yield*/, this.removeAttribute(attributeName, { scope: targetScope, quiet: true })]; | ||
case 2: | ||
_d.sent(); | ||
_d.label = 3; | ||
case 3: | ||
_b++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [4 /*yield*/, this.readScoring(targetScope)]; | ||
case 5: | ||
currentValue = _d.sent(); | ||
return [4 /*yield*/, this.applyScoring(targetScope, currentValue, valueToAdd, addedAttributes.length)]; | ||
case 6: | ||
_d.sent(); | ||
if (!!(options === null || options === void 0 ? void 0 : options.quiet)) return [3 /*break*/, 8]; | ||
return [4 /*yield*/, this.reevaluateSignals()]; | ||
case 7: | ||
_d.sent(); | ||
_d.label = 8; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
OptimizeTracker.prototype.getAttributeValues = function (attributeName, options) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var isKnown, current, values; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
isKnown = this.isKnownAttribute(attributeName); | ||
if (!isKnown) { | ||
return [2 /*return*/, undefined]; | ||
} | ||
return [4 /*yield*/, this.getIntentStrength(options === null || options === void 0 ? void 0 : options.scope)]; | ||
case 1: | ||
current = (_a.sent()) || {}; | ||
values = Object.keys(current || {}).reduce(function (previous, key) { | ||
var type = getAttributeValue(attributeName, key); | ||
if (type && current[key].str) { | ||
previous[type] = Number(current[key].str || 0); | ||
} | ||
return previous; | ||
}, {}); | ||
return [2 /*return*/, values]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return OptimizeTracker; | ||
}()); | ||
export { OptimizeTracker }; |
@@ -9,1 +9,4 @@ import { TrackerLogLevels } from '@uniformdev/optimize-tracker-common'; | ||
export declare const isLogLevelUnderThreshold: (threshold: TrackerLogLevels, level: TrackerLogLevels) => boolean; | ||
export declare const attributeDivider = ">"; | ||
export declare const calculateAttributeIntentName: (attributeType: string, attributeValue: string) => string; | ||
export declare const getAttributeValue: (attributeType: string, storedName: string) => string | undefined; |
@@ -15,1 +15,14 @@ export var logLevelMap = { | ||
}; | ||
export var attributeDivider = '>'; | ||
export var calculateAttributeIntentName = function (attributeType, attributeValue) { | ||
if (attributeType.includes(attributeDivider) || attributeValue.includes(attributeDivider)) { | ||
throw "\"" + attributeDivider + "\" is a reserved attribute character"; | ||
} | ||
return "" + attributeType + attributeDivider + attributeValue; | ||
}; | ||
export var getAttributeValue = function (attributeType, storedName) { | ||
var _a = storedName.split(attributeDivider), type = _a[0], value = _a[1]; | ||
if (type === attributeType) { | ||
return value; | ||
} | ||
}; |
{ | ||
"name": "@uniformdev/optimize-tracker", | ||
"version": "2.0.1-alpha.34+2992349", | ||
"version": "2.0.1-alpha.35+9771b51", | ||
"description": "Uniform Optimize tracker", | ||
@@ -29,4 +29,4 @@ "license": "Apache-2.0", | ||
"dependencies": { | ||
"@uniformdev/optimize-common": "^2.0.1-alpha.34+2992349", | ||
"@uniformdev/optimize-tracker-common": "^2.0.1-alpha.34+2992349" | ||
"@uniformdev/optimize-common": "^2.0.1-alpha.35+9771b51", | ||
"@uniformdev/optimize-tracker-common": "^2.0.1-alpha.35+9771b51" | ||
}, | ||
@@ -56,3 +56,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "2992349c203b538ab66c42fc5458e4f02cb05796" | ||
"gitHead": "9771b516ca4e9197d234d0d2a7c16669443d4c46" | ||
} |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
259802
5370