@optimizely/optimizely-sdk
Advanced tools
Comparing version 3.2.0-beta to 3.2.0
@@ -10,2 +10,62 @@ # Changelog | ||
## [3.2.0] - May 30th, 2019 | ||
### New Features | ||
- Added support for automatic datafile management ([#261](https://github.com/optimizely/javascript-sdk/pull/261)), ([#266](https://github.com/optimizely/javascript-sdk/pull/266)), ([#267](https://github.com/optimizely/javascript-sdk/pull/267)), ([#268](https://github.com/optimizely/javascript-sdk/pull/268)), ([#270](https://github.com/optimizely/javascript-sdk/pull/270)), ([#272](https://github.com/optimizely/javascript-sdk/pull/272)) | ||
- To use automatic datafile management, include `sdkKey` as a string property in the options object you pass to `createInstance`. | ||
- When sdkKey is provided, the SDK instance will download the datafile associated with that sdkKey immediately upon construction. When the download completes, the SDK instance will update itself to use the downloaded datafile. | ||
- Use the `onReady` method to wait until the download is complete and the SDK is ready to use. | ||
- Customize datafile management behavior by passing a `datafileOptions` object within the options you pass to `createInstance`. | ||
- Enable automatic updates by passing `autoUpdate: true`. Periodically (on the provided update interval), the SDK instance will download the datafile and update itself. Use this to ensure that the SDK instance is using a fresh datafile reflecting changes recently made to your experiment or feature configuration. | ||
- Add a notification listener for the `OPTIMIZELY_CONFIG_UPDATE` notification type to be notified when an instance updates its Optimizely config after obtaining a new datafile. | ||
- Stop active downloads and cancel recurring downloads by calling the `close` method | ||
#### Create an instance with datafile management enabled | ||
```js | ||
const optimizely = require('@optimizely/optimizely-sdk'); | ||
const optimizelyClientInstance = optimizely.createInstance({ | ||
sdkKey: '12345', // Provide the sdkKey of your desired environment here | ||
}); | ||
``` | ||
#### Use `onReady` to wait until optimizelyClientInstance has a datafile | ||
```js | ||
const optimizely = require('@optimizely/optimizely-sdk'); | ||
const optimizelyClientInstance = optimizely.createInstance({ | ||
sdkKey: '12345', | ||
}); | ||
optimizelyClientInstance.onReady().then(() => { | ||
// optimizelyClientInstance is ready to use, with datafile downloaded from the Optimizely CDN | ||
}); | ||
``` | ||
#### Enable automatic updates, add notification listener for OPTIMIZELY_CONFIG_UPDATE notification type, and stop automatic updates | ||
```js | ||
const optimizely = require('@optimizely/optimizely-sdk'); | ||
const optimizelyClientInstance = optimizely.createInstance({ | ||
sdkKey: '12345', | ||
datafileOptions: { | ||
autoUpdate: true, | ||
updateInterval: 600000 // 10 minutes in milliseconds | ||
}, | ||
}); | ||
optimizelyClientInstance.notificationCenter.addNotificationListener( | ||
optimizely.enums.NOTIFICATION_TYPES.OPTIMIZELY_CONFIG_UPDATE, | ||
() => { | ||
// optimizelyClientInstance has updated its Optimizely config | ||
}, | ||
); | ||
// Stop automatic updates - optimizelyClientInstance will use whatever datafile it currently has from now on | ||
optimizelyClientInstance.close(); | ||
``` | ||
### Changed | ||
- Forced variation logic has been moved from the project config module to the decision service. Prefixes for forced-variation-related log messages will reflect this change ([#261](https://github.com/optimizely/javascript-sdk/pull/261)). | ||
- Update TypeScript definitions to account for new methods (`onReady`, `close`) and new properties on object accepted by createInstance (`datafileOptions`, `sdkKey`), ([#263](https://github.com/optimizely/javascript-sdk/pull/263)), ([#278](https://github.com/optimizely/javascript-sdk/pull/278)) | ||
- Allow react-sdk to be passed in as `clientEngine` ([#279](https://github.com/optimizely/javascript-sdk/pull/279)) | ||
### Bug Fixes: | ||
- Add logging message for `optimizely.track()` ([#281](https://github.com/optimizely/javascript-sdk/pull/281)) | ||
## [3.2.0-beta] - May 16th, 2019 | ||
@@ -12,0 +72,0 @@ |
@@ -96,5 +96,6 @@ /** | ||
config = fns.assignIn({}, config, { | ||
config = fns.assignIn({ | ||
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, | ||
}, config, { | ||
eventDispatcher: wrappedEventDispatcher, | ||
clientEngine: enums.JAVASCRIPT_CLIENT_ENGINE, | ||
// always get the OptimizelyLogger facade from logging | ||
@@ -101,0 +102,0 @@ logger: logger, |
@@ -122,3 +122,3 @@ /** | ||
assert.instanceOf(optlyInstance, Optimizely); | ||
assert.equal(optlyInstance.clientVersion, '3.2.0-beta'); | ||
assert.equal(optlyInstance.clientVersion, '3.2.0'); | ||
}); | ||
@@ -139,2 +139,28 @@ | ||
it('should allow passing of "react-sdk" as the clientEngine', function() { | ||
var optlyInstance = optimizelyFactory.createInstance({ | ||
clientEngine: 'react-sdk', | ||
datafile: {}, | ||
errorHandler: fakeErrorHandler, | ||
eventDispatcher: fakeEventDispatcher, | ||
logger: silentLogger, | ||
}); | ||
// Invalid datafile causes onReady Promise rejection - catch this error | ||
optlyInstance.onReady().catch(function() {}); | ||
assert.equal('react-sdk', optlyInstance.clientEngine); | ||
}); | ||
it('should allow passing of "react-sdk" as the clientEngine', function() { | ||
var optlyInstance = optimizelyFactory.createInstance({ | ||
clientEngine: 'react-sdk', | ||
datafile: {}, | ||
errorHandler: fakeErrorHandler, | ||
eventDispatcher: fakeEventDispatcher, | ||
logger: silentLogger, | ||
}); | ||
// Invalid datafile causes onReady Promise rejection - catch this error | ||
optlyInstance.onReady().catch(function() {}); | ||
assert.equal('react-sdk', optlyInstance.clientEngine); | ||
}); | ||
it('should activate with provided event dispatcher', function() { | ||
@@ -141,0 +167,0 @@ var optlyInstance = optimizelyFactory.createInstance({ |
/** | ||
* Copyright 2018, Optimizely | ||
* Copyright 2018-2019, Optimizely | ||
* | ||
@@ -17,196 +17,234 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
declare module '@optimizely/optimizely-sdk' { | ||
export namespace enums { | ||
enum LOG_LEVEL { | ||
NOTSET = 0, | ||
DEBUG = 1, | ||
INFO = 2, | ||
WARNING = 3, | ||
ERROR = 4, | ||
} | ||
declare module "@optimizely/optimizely-sdk" { | ||
import { LogHandler, ErrorHandler } from "@optimizely/js-sdk-logging"; | ||
import * as enums from "@optimizely/optimizely-sdk/lib/utils/enums"; | ||
import * as logging from "@optimizely/optimizely-sdk/lib/plugins/logger"; | ||
export { enums, logging }; | ||
enum NOTIFICATION_TYPES { | ||
ACTIVATE = 'ACTIVATE:experiment, user_id,attributes, variation, event', | ||
DECISION = 'DECISION:type, userId, attributes, decisionInfo', | ||
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE', | ||
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event', | ||
} | ||
} | ||
export function setLogger(logger: LogHandler | null): void; | ||
export function createInstance(config: Config): Client; | ||
export function setLogLevel(level: enums.LOG_LEVEL | string): void; | ||
interface DatafileOptions { | ||
autoUpdate?: boolean; | ||
updateInterval?: number; | ||
urlTemplate?: string; | ||
} | ||
export function createInstance(config: Config): Client; | ||
// The options object given to Optimizely.createInstance. | ||
export interface Config { | ||
datafile?: object | string; | ||
datafileOptions?: DatafileOptions, | ||
errorHandler?: object; | ||
eventDispatcher?: object; | ||
logger?: object; | ||
logLevel?: enums.LOG_LEVEL.DEBUG | enums.LOG_LEVEL.ERROR | enums.LOG_LEVEL.INFO | enums.LOG_LEVEL.NOTSET | enums.LOG_LEVEL.WARNING; | ||
skipJSONValidation?: boolean; | ||
jsonSchemaValidator?: object; | ||
userProfileService?: UserProfileService | null; | ||
eventBatchSize?: number | ||
eventFlushInterval?: number | ||
sdkKey?: string; | ||
} | ||
export const errorHandler: ErrorHandler; | ||
export interface Client { | ||
notificationCenter: NotificationCenter; | ||
activate(experimentKey: string, userId: string, attributes?: UserAttributes): string | null; | ||
track(eventKey: string, userId: string, attributes?: UserAttributes, eventTags?: EventTags): void; | ||
getVariation(experimentKey: string, userId: string, attributes?: UserAttributes): string | null; | ||
setForcedVariation(experimentKey: string, userId: string, variationKey: string | null): boolean; | ||
getForcedVariation(experimentKey: string, userId: string): string | null; | ||
isFeatureEnabled(featureKey: string, userId: string, attributes?: UserAttributes): boolean; | ||
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[]; | ||
getFeatureVariableBoolean(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): boolean | null; | ||
getFeatureVariableDouble(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null; | ||
getFeatureVariableInteger(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): number | null; | ||
getFeatureVariableString(featureKey: string, variableKey: string, userId: string, attributes?: UserAttributes): string | null; | ||
onReady(options?: { timeout?: number }): Promise<void> | ||
close(): void | ||
} | ||
export const eventDispatcher: EventDispatcher; | ||
// An event to be submitted to Optimizely, enabling tracking the reach and impact of | ||
// tests and feature rollouts. | ||
export interface Event { | ||
// URL to which to send the HTTP request. | ||
url: string, | ||
// HTTP method with which to send the event. | ||
httpVerb: 'POST', | ||
// Value to send in the request body, JSON-serialized. | ||
params: any, | ||
} | ||
interface DatafileOptions { | ||
autoUpdate?: boolean; | ||
updateInterval?: number; | ||
urlTemplate?: string; | ||
} | ||
export interface EventDispatcher { | ||
/** | ||
* @param event | ||
* Event being submitted for eventual dispatch. | ||
* @param callback | ||
* After the event has at least been queued for dispatch, call this function to return | ||
* control back to the Client. | ||
*/ | ||
dispatchEvent: (event: Event, callback: () => void) => void, | ||
} | ||
// The options object given to Optimizely.createInstance. | ||
export interface Config { | ||
datafile?: object | string; | ||
datafileOptions?: DatafileOptions; | ||
errorHandler?: ErrorHandler; | ||
eventDispatcher?: EventDispatcher; | ||
logger?: LogHandler; | ||
logLevel?: | ||
| enums.LOG_LEVEL.DEBUG | ||
| enums.LOG_LEVEL.ERROR | ||
| enums.LOG_LEVEL.INFO | ||
| enums.LOG_LEVEL.NOTSET | ||
| enums.LOG_LEVEL.WARNING; | ||
skipJSONValidation?: boolean; | ||
jsonSchemaValidator?: object; | ||
userProfileService?: UserProfileService | null; | ||
eventBatchSize?: number; | ||
eventFlushInterval?: number; | ||
sdkKey?: string; | ||
} | ||
export interface UserProfileService { | ||
lookup: (userId: string) => UserProfile, | ||
save: (profile: UserProfile) => void, | ||
} | ||
export interface Client { | ||
notificationCenter: NotificationCenter; | ||
activate( | ||
experimentKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): string | null; | ||
track( | ||
eventKey: string, | ||
userId: string, | ||
attributes?: UserAttributes, | ||
eventTags?: EventTags | ||
): void; | ||
getVariation( | ||
experimentKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): string | null; | ||
setForcedVariation( | ||
experimentKey: string, | ||
userId: string, | ||
variationKey: string | null | ||
): boolean; | ||
getForcedVariation(experimentKey: string, userId: string): string | null; | ||
isFeatureEnabled( | ||
featureKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): boolean; | ||
getEnabledFeatures(userId: string, attributes?: UserAttributes): string[]; | ||
getFeatureVariableBoolean( | ||
featureKey: string, | ||
variableKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): boolean | null; | ||
getFeatureVariableDouble( | ||
featureKey: string, | ||
variableKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): number | null; | ||
getFeatureVariableInteger( | ||
featureKey: string, | ||
variableKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): number | null; | ||
getFeatureVariableString( | ||
featureKey: string, | ||
variableKey: string, | ||
userId: string, | ||
attributes?: UserAttributes | ||
): string | null; | ||
onReady(options?: { | ||
timeout?: number; | ||
}): Promise<{ success: boolean; reason?: string }>; | ||
close(): void; | ||
} | ||
// NotificationCenter-related types | ||
export interface NotificationCenter { | ||
addNotificationListener<T extends ListenerPayload>(notificationType: string, callback: NotificationListener<T>): number; | ||
removeNotificationListener(listenerId: number): boolean; | ||
clearAllNotificationListeners(): void; | ||
clearNotificationListeners(notificationType: enums.NOTIFICATION_TYPES): void; | ||
} | ||
// An event to be submitted to Optimizely, enabling tracking the reach and impact of | ||
// tests and feature rollouts. | ||
export interface Event { | ||
// URL to which to send the HTTP request. | ||
url: string; | ||
// HTTP method with which to send the event. | ||
httpVerb: "POST"; | ||
// Value to send in the request body, JSON-serialized. | ||
params: any; | ||
} | ||
export type NotificationListener<T extends ListenerPayload> = (notificationData: T) => void; | ||
export interface EventDispatcher { | ||
/** | ||
* @param event | ||
* Event being submitted for eventual dispatch. | ||
* @param callback | ||
* After the event has at least been queued for dispatch, call this function to return | ||
* control back to the Client. | ||
*/ | ||
dispatchEvent: (event: Event, callback: () => void) => void; | ||
} | ||
export interface ListenerPayload { | ||
userId: string; | ||
attributes: UserAttributes; | ||
} | ||
export interface UserProfileService { | ||
lookup: (userId: string) => UserProfile; | ||
save: (profile: UserProfile) => void; | ||
} | ||
export interface ActivateListenerPayload extends ListenerPayload { | ||
experiment: Experiment; | ||
variation: Variation; | ||
logEvent: Event; | ||
} | ||
// NotificationCenter-related types | ||
export interface NotificationCenter { | ||
addNotificationListener<T extends ListenerPayload>( | ||
notificationType: string, | ||
callback: NotificationListener<T> | ||
): number; | ||
removeNotificationListener(listenerId: number): boolean; | ||
clearAllNotificationListeners(): void; | ||
clearNotificationListeners( | ||
notificationType: enums.NOTIFICATION_TYPES | ||
): void; | ||
} | ||
export type UserAttributes = { | ||
[name: string]: string | ||
}; | ||
export type NotificationListener<T extends ListenerPayload> = ( | ||
notificationData: T | ||
) => void; | ||
export type EventTags = { | ||
[key: string]: string | number | boolean, | ||
}; | ||
export interface ListenerPayload { | ||
userId: string; | ||
attributes: UserAttributes; | ||
} | ||
export interface TrackListenerPayload extends ListenerPayload { | ||
eventKey: string; | ||
eventTags: EventTags; | ||
logEvent: Event; | ||
} | ||
export interface ActivateListenerPayload extends ListenerPayload { | ||
experiment: Experiment; | ||
variation: Variation; | ||
logEvent: Event; | ||
} | ||
interface Experiment { | ||
id: string, | ||
key: string, | ||
status: string, | ||
layerId: string, | ||
variations: Variation[], | ||
trafficAllocation: Array<{ | ||
entityId: string, | ||
endOfRange: number, | ||
}>, | ||
audienceIds: string[], | ||
forcedVariations: object, | ||
} | ||
export type UserAttributes = { | ||
[name: string]: any; | ||
}; | ||
interface Variation { | ||
id: string, | ||
key: string, | ||
} | ||
export type EventTags = { | ||
[key: string]: string | number | boolean; | ||
}; | ||
export interface Logger { | ||
log: (logLevel: enums.LOG_LEVEL, message: string) => void, | ||
} | ||
export interface TrackListenerPayload extends ListenerPayload { | ||
eventKey: string; | ||
eventTags: EventTags; | ||
logEvent: Event; | ||
} | ||
// Information about past bucketing decisions for a user. | ||
export interface UserProfile { | ||
user_id: string, | ||
experiment_bucket_map: { | ||
[experiment_id: string]: { | ||
variation_id: string, | ||
}, | ||
}, | ||
} | ||
interface Experiment { | ||
id: string; | ||
key: string; | ||
status: string; | ||
layerId: string; | ||
variations: Variation[]; | ||
trafficAllocation: Array<{ | ||
entityId: string; | ||
endOfRange: number; | ||
}>; | ||
audienceIds: string[]; | ||
forcedVariations: object; | ||
} | ||
declare module '@optimizely/optimizely-sdk/lib/utils/enums'{ | ||
export enum LOG_LEVEL { | ||
NOTSET = 0, | ||
DEBUG = 1, | ||
INFO = 2, | ||
WARNING = 3, | ||
ERROR = 4, | ||
} | ||
interface Variation { | ||
id: string; | ||
key: string; | ||
} | ||
export enum NOTIFICATION_TYPES { | ||
ACTIVATE = 'ACTIVATE:experiment, user_id,attributes, variation, event', | ||
DECISION = 'DECISION:type, userId, attributes, decisionInfo', | ||
OPTIMIZELY_CONFIG_UPDATE = 'OPTIMIZELY_CONFIG_UPDATE', | ||
TRACK = 'TRACK:event_key, user_id, attributes, event_tags, event', | ||
} | ||
// Information about past bucketing decisions for a user. | ||
export interface UserProfile { | ||
user_id: string; | ||
experiment_bucket_map: { | ||
[experiment_id: string]: { | ||
variation_id: string; | ||
}; | ||
}; | ||
} | ||
} | ||
declare module '@optimizely/optimizely-sdk/lib/plugins/event_dispatcher/index.node.js' { | ||
declare module "@optimizely/optimizely-sdk/lib/utils/enums" { | ||
import { LogLevel } from "@optimizely/js-sdk-logging"; | ||
export { LogLevel as LOG_LEVEL }; | ||
export enum NOTIFICATION_TYPES { | ||
ACTIVATE = "ACTIVATE:experiment, user_id,attributes, variation, event", | ||
DECISION = "DECISION:type, userId, attributes, decisionInfo", | ||
OPTIMIZELY_CONFIG_UPDATE = "OPTIMIZELY_CONFIG_UPDATE", | ||
TRACK = "TRACK:event_key, user_id, attributes, event_tags, event" | ||
} | ||
} | ||
declare module '@optimizely/optimizely-sdk/lib/utils/json_schema_validator' { | ||
declare module "@optimizely/optimizely-sdk/lib/plugins/logger" { | ||
import * as enums from "@optimizely/optimizely-sdk/lib/utils/enums"; | ||
import { LogHandler } from "@optimizely/js-sdk-logging"; | ||
export interface LoggerConfig { | ||
logLevel?: enums.LOG_LEVEL; | ||
logToConsole?: boolean; | ||
prefix?: string; | ||
} | ||
export function createLogger(config?: LoggerConfig): LogHandler; | ||
export function createNoOpLogger(): LogHandler; | ||
} | ||
declare module '@optimizely/optimizely-sdk/lib/plugins/error_handler' { | ||
} | ||
declare module "@optimizely/optimizely-sdk/lib/plugins/event_dispatcher" {} | ||
declare module '@optimizely/optimizely-sdk/lib/plugins/logger' { | ||
import * as Optimizely from '@optimizely/optimizely-sdk'; | ||
import * as enums from '@optimizely/optimizely-sdk/lib/utils/enums'; | ||
declare module "@optimizely/optimizely-sdk/lib/utils/json_schema_validator" {} | ||
export interface Config { | ||
logLevel?: enums.LOG_LEVEL, | ||
logToConsole?: boolean, | ||
prefix?: string, | ||
} | ||
export function createLogger(config: Config): Optimizely.Logger; | ||
export function createNoOpLogger(): Optimizely.Logger; | ||
} | ||
declare module "@optimizely/optimizely-sdk/lib/plugins/error_handler" {} |
@@ -89,2 +89,3 @@ /**************************************************************************** | ||
{ | ||
clientEngine: enums.NODE_CLIENT_ENGINE, | ||
eventDispatcher: defaultEventDispatcher, | ||
@@ -96,3 +97,2 @@ jsonSchemaValidator: jsonSchemaValidator, | ||
{ | ||
clientEngine: enums.NODE_CLIENT_ENGINE, | ||
// always get the OptimizelyLogger facade from logging | ||
@@ -99,0 +99,0 @@ logger: logger, |
@@ -93,3 +93,3 @@ /** | ||
assert.instanceOf(optlyInstance, Optimizely); | ||
assert.equal(optlyInstance.clientVersion, '3.2.0-beta'); | ||
assert.equal(optlyInstance.clientVersion, '3.2.0'); | ||
}); | ||
@@ -96,0 +96,0 @@ }); |
@@ -61,3 +61,3 @@ /**************************************************************************** | ||
var clientEngine = config.clientEngine; | ||
if (clientEngine !== enums.NODE_CLIENT_ENGINE && clientEngine !== enums.JAVASCRIPT_CLIENT_ENGINE) { | ||
if (enums.VALID_CLIENT_ENGINES.indexOf(clientEngine) === -1) { | ||
config.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.INVALID_CLIENT_ENGINE, MODULE_NAME, clientEngine)); | ||
@@ -303,2 +303,3 @@ clientEngine = enums.NODE_CLIENT_ENGINE; | ||
}); | ||
this.logger.log(LOG_LEVEL.INFO, sprintf(enums.LOG_MESSAGES.TRACK_EVENT, MODULE_NAME, eventKey, userId)); | ||
// TODO is it okay to not pass a projectConfig as second argument | ||
@@ -305,0 +306,0 @@ this.eventProcessor.process(conversionEvent); |
@@ -157,4 +157,11 @@ /**************************************************************************** | ||
exports.NODE_CLIENT_ENGINE = 'node-sdk'; | ||
exports.NODE_CLIENT_VERSION = '3.2.0-beta'; | ||
exports.REACT_CLIENT_ENGINE = 'react-sdk'; | ||
exports.NODE_CLIENT_VERSION = '3.2.0'; | ||
exports.VALID_CLIENT_ENGINES = [ | ||
exports.NODE_CLIENT_ENGINE, | ||
exports.REACT_CLIENT_ENGINE, | ||
exports.JAVASCRIPT_CLIENT_ENGINE, | ||
]; | ||
/* | ||
@@ -161,0 +168,0 @@ * Notification types for use with NotificationCenter |
{ | ||
"name": "@optimizely/optimizely-sdk", | ||
"version": "3.2.0-beta", | ||
"version": "3.2.0", | ||
"description": "JavaScript SDK for Optimizely X Full Stack", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.node.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1363425
30188
1