-
Notifications
You must be signed in to change notification settings - Fork 42
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
Closed
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
84229ac
Use v2 analytics
kyle-ssg c825c8d
Add track event call
kyle-ssg 08ba379
flush analytics before sending conversion events
kyle-ssg e898725
Merge branch 'chore/fix-lost-event-emitters' into chore/v2-analytics
kyle-ssg 951dd13
merge events fix
kyle-ssg dd6bd15
bump
kyle-ssg 1aa3e0c
Fix analytics endpoint
kyle-ssg 9cef36f
Merge branch 'refs/heads/main' into chore/v2-analytics
kyle-ssg e035b4d
Change version to minor
kyle-ssg a4371e3
re-add readme
kyle-ssg 5d254be
Version bumps
kyle-ssg 9c377ce
Add track event types
kyle-ssg 3a23dd7
Add analytics tests
kyle-ssg 3c13bca
Adjust test
kyle-ssg ded79ff
Make analytics backwards compatible
kyle-ssg db37d65
private parseV2Analytics
kyle-ssg 54d3889
tidy
kyle-ssg 1936779
remove only
kyle-ssg ebfbf9d
rename parseV2Analytics
kyle-ssg 62ce8c3
Merge branch 'main' into feat/v2-analytics
kyle-ssg fc71031
Merge latest SDK
kyle-ssg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}, | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.