Skip to content

feat: v2 analytics and split testing #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 102 additions & 48 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ const FLAGSMITH_CONFIG_ANALYTICS_KEY = "flagsmith_value_";
const FLAGSMITH_FLAG_ANALYTICS_KEY = "flagsmith_enabled_";
const FLAGSMITH_TRAIT_ANALYTICS_KEY = "flagsmith_trait_";


/*API configuration includes the /v1/
This function replaces that version with another.
In future, we may exclude /v1/ from api configuration however this would be a breaking change*/
function apiVersion(api:string, version:number) {
return api.replace("/v1/",`/v${version}/`)
}
const Flagsmith = class {
_trigger?:(()=>void)|null= null
_triggerLoadingState?:(()=>void)|null= null
Expand All @@ -75,6 +82,11 @@ const Flagsmith = class {
}
}


events: string[] = []

splitTestingAnalytics=false;

getFlags = () => {
const { identity, api } = this;
this.log("Get Flags")
Expand Down Expand Up @@ -206,32 +218,6 @@ const Flagsmith = class {
}
};

analyticsFlags = () => {
const { api } = this;

if (!this.evaluationEvent || !this.evaluationEvent[this.environmentID]) {
return
}

if (this.evaluationEvent && Object.getOwnPropertyNames(this.evaluationEvent).length !== 0 && Object.getOwnPropertyNames(this.evaluationEvent[this.environmentID]).length !== 0) {
return this.getJSON(api + 'analytics/flags/', 'POST', JSON.stringify(this.evaluationEvent[this.environmentID]))
.then((res) => {
const state = this.getState();
if (!this.evaluationEvent) {
this.evaluationEvent = {}
}
this.evaluationEvent[this.environmentID] = {}
this.setState({
...state,
evaluationEvent: this.evaluationEvent,
});
this.updateEventStorage();
}).catch((err) => {
this.log("Exception fetching evaluationEvent", err);
});
}
};

datadogRum: IDatadogRum | null = null;
loadingState: LoadingState = {isLoading: true, isFetching: true, error: null, source: FlagSource.NONE}
canUseStorage = false
Expand Down Expand Up @@ -260,31 +246,33 @@ const Flagsmith = class {
async init(config: IInitConfig) {
try {
const {
environmentID,
AsyncStorage: _AsyncStorage,
angularHttpClient,
api = defaultAPI,
headers,
onChange,
cacheFlags,
cacheOptions,
datadogRum,
onError,
defaultFlags,
enableAnalytics,
enableDynatrace,
enableLogs,
environmentID,
eventSourceUrl= "https://realtime.flagsmith.com/",
fetch: fetchImplementation,
headers,
identity,
onChange,
onError,
preventFetch,
enableLogs,
enableDynatrace,
enableAnalytics,
splitTestingAnalytics,
realtime,
eventSourceUrl= "https://realtime.flagsmith.com/",
AsyncStorage: _AsyncStorage,
identity,
traits,
state,
cacheOptions,
angularHttpClient,
traits,
_trigger,
_triggerLoadingState,
} = config;
this.environmentID = environmentID;
this.splitTestingAnalytics = !!splitTestingAnalytics;
this.api = api;
this.headers = headers;
this.getFlagInterval = null;
Expand Down Expand Up @@ -504,15 +492,6 @@ const Flagsmith = class {
}
}

private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
return {
error,
isFetching,
isLoading: false,
source
}
}

getAllFlags() {
return this.flags;
}
Expand All @@ -523,6 +502,7 @@ const Flagsmith = class {
this.withTraits = {}
}
this.identity = userId;
this.events.map(this.trackEvent)
this.log("Identify: " + this.identity)

if (traits) {
Expand Down Expand Up @@ -681,12 +661,86 @@ const Flagsmith = class {
return res;
};

trackEvent = (event: string) => {
if (!this.splitTestingAnalytics) {
const error = new Error("This feature is only enabled for self-hosted customers using split testing.");
console.error(error.message);
return Promise.reject(error);
} else if (!this.identity) {
this.events.push(event);
this.log("Waiting for user to be identified before tracking event", event);
return Promise.resolve();
} else {
return this.analyticsFlags().then(() => {
return this.getJSON(this.api + 'split-testing/conversion-events/', "POST", JSON.stringify({
'identity_identifier': this.identity,
'type': event
}));
});
}
};

private _loadedState(error: any = null, source: FlagSource, isFetching = false) {
return {
error,
isFetching,
isLoading: false,
source
}
}

private analyticsFlags = () => {
const { api } = this;

if (!this.evaluationEvent|| !this.evaluationEvent[this.environmentID] || !api) {
return Promise.resolve()
}

if (this.evaluationEvent && Object.getOwnPropertyNames(this.evaluationEvent).length !== 0 && Object.getOwnPropertyNames(this.evaluationEvent[this.environmentID]).length !== 0) {
return this.getJSON(apiVersion(`${api}`, this.splitTestingAnalytics?2:1) + 'analytics/flags/',
'POST',
JSON.stringify(this.parseV2Analytics(this.evaluationEvent[this.environmentID])))
.then((res) => {
const state = this.getState();
if (!this.evaluationEvent) {
this.evaluationEvent = {}
}
this.evaluationEvent[this.environmentID] = {}
this.setState({
...state,
evaluationEvent: this.evaluationEvent,
});
this.updateEventStorage();
}).catch((err) => {
this.log("Exception fetching evaluationEvent", err);
});
}
return Promise.resolve()
};

private log(...args: (unknown)[]) {
if (this.enableLogs) {
console.log.apply(this, ['FLAGSMITH:', new Date().valueOf() - (this.timer || 0), 'ms', ...args]);
}
}

private parseV2Analytics = (evaluations: Record<string, number>|null)=> {
if(!this.splitTestingAnalytics) {
return evaluations || {}
}
if(!evaluations) return {evaluations: []}
return {
evaluations: Object.keys(evaluations).map((feature_name)=>(
{
feature_name,
"identity_identifier": this.identity||null,
"count": evaluations[feature_name],
"enabled_when_evaluated": this.hasFeature(feature_name),
}
))
}
};

private updateStorage() {
if (this.cacheFlags) {
this.ts = new Date().valueOf();
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith-es/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith-es",
"version": "4.1.1",
"version": "4.2.0",
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
"main": "./index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith",
"version": "4.1.1",
"version": "4.2.0",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion lib/react-native-flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-flagsmith",
"version": "4.1.1",
"version": "4.2.0",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
116 changes: 116 additions & 0 deletions test/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Sample test
import { getFlagsmith, getMockFetchWithValue, testIdentity } from './test-constants';

describe('Analytics', () => {

beforeEach(() => {
jest.useFakeTimers(); // Mocked to allow time to pass for analytics flush
});
afterEach(() => {
jest.useRealTimers();
});
test('should not attempt to track events when split testing is disabled', async () => {
const { flagsmith } = getFlagsmith({
cacheFlags: true,
identity: testIdentity,
enableAnalytics: true,
splitTestingAnalytics: false, // Disable split testing
});

await expect(flagsmith.trackEvent("checkout"))
.rejects.toThrow('This feature is only enabled for self-hosted customers using split testing.');
});
test('should track v1 analytics', async () => {
const onChange = jest.fn();
const fetchFn = getMockFetchWithValue({
flags:[{feature:{name:"font_size"}, enabled: true}, {feature:{name:"off_value"}, enabled: false}],
});
const { flagsmith, initConfig, mockFetch } = getFlagsmith({
cacheFlags: true,
identity: testIdentity,
enableAnalytics: true,
onChange,
}, fetchFn);
await flagsmith.init(initConfig);
flagsmith.getValue("font_size")
flagsmith.hasFeature("off_value")
flagsmith.hasFeature("off_value")
jest.advanceTimersByTime(10000);
expect(mockFetch).toHaveBeenCalledWith(
`${flagsmith.api}analytics/flags/`,
{
method: 'POST',
body: JSON.stringify({
font_size: 1,
off_value: 2,
}),
cache: 'no-cache',
headers: {
'x-environment-key': flagsmith.environmentID,
'Content-Type': 'application/json; charset=utf-8',
},
},
);
});
test('should track conversion events when trackEvent is called', async () => {
const onChange = jest.fn();
const fetchFn = getMockFetchWithValue({
flags:[{feature:{name:"font_size"}, enabled: true}, {feature:{name:"off_value"}, enabled: false}],
});
const { flagsmith, initConfig, mockFetch } = getFlagsmith({
cacheFlags: true,
identity: testIdentity,
enableAnalytics: true,
splitTestingAnalytics: true,
onChange,
}, fetchFn);
await flagsmith.init(initConfig);
flagsmith.getValue("font_size")
flagsmith.hasFeature("off_value")
flagsmith.hasFeature("off_value")
await flagsmith.trackEvent('checkout');

expect(mockFetch).toHaveBeenCalledWith(
`${flagsmith.api}split-testing/conversion-events/`,
{
method: 'POST',
body: JSON.stringify({
identity_identifier: testIdentity,
type: 'checkout',
}),
cache: 'no-cache',
headers: {
'x-environment-key': flagsmith.environmentID,
'Content-Type': 'application/json; charset=utf-8',
},
},
);
expect(mockFetch).toHaveBeenCalledWith(
`${flagsmith.api.replace('/v1/', '/v2/')}analytics/flags/`,
{
method: 'POST',
body: JSON.stringify({
"evaluations": [
{
"feature_name": "font_size",
"identity_identifier": "test_identity",
"count": 1,
"enabled_when_evaluated": true
},
{
"feature_name": "off_value",
"identity_identifier": "test_identity",
"count": 2,
"enabled_when_evaluated": false
}
]
}),
cache: 'no-cache',
headers: {
'x-environment-key': flagsmith.environmentID,
'Content-Type': 'application/json; charset=utf-8',
},
},
);
});
});
Loading