-
Notifications
You must be signed in to change notification settings - Fork 44
feat: collect-and-flush-analytics-evaluation-events #377
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
Open
Zaimwa9
wants to merge
24
commits into
main
Choose a base branch
from
feat/send-evaluation-data-to-analytics-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
02573c6
feat: collect-and-flush-analytics-evaluation-events
Zaimwa9 966faee
fix: race-condition-and-cleanup
Zaimwa9 180c55d
feat: added-pull-request-template
Zaimwa9 3dbf39f
feat: added-publish-internal-workflow-oidc-compatible
Zaimwa9 bb88e0c
feat: remap-events-to-latest-schema
Zaimwa9 f2998b0
feat: cleaned-up-endpoint-fetch-mock
Zaimwa9 9e9c844
feat: cleaned-up-unused-variables-and-types
Zaimwa9 afea352
feat: added-page-url-in-metadata
Zaimwa9 c7faf16
feat: sync-payload-with-expected-rust
Zaimwa9 7e7dd4c
feat: removed-pipeline-action-and-template
Zaimwa9 ee8143a
feat: removed-test-asserting-data-depending-on-ci
Zaimwa9 fa34dc0
feat: suffix-internal-version-in-action
Zaimwa9 2ee7d80
feat: removing-internal-action-bis
Zaimwa9 6e2e3f5
Merge branch 'main' of github.com:Flagsmith/flagsmith-js-client into …
Zaimwa9 f851334
Merge branch 'main' of github.com:Flagsmith/flagsmith-js-client into …
Zaimwa9 7f288cc
feat: deduplicate-events-using-a-field-fingerprint
Zaimwa9 9bc48ca
feat: temporarily-empty-strings-for-undefined-strings
Zaimwa9 86ec20c
feat: added-experimental-and-hidden-tags
Zaimwa9 6d5d376
feat: removed-identifier-non-nullable-workaround
Zaimwa9 cd37225
feat: removed-identifier-non-nullable-workaround
Zaimwa9 b794a37
Merge branch 'feat/send-evaluation-data-to-analytics-pipeline' of git…
Zaimwa9 36066b2
fix: removed-trim-pipeline-buffer-to-prevent-event-loss
Zaimwa9 e520b87
Merge branch 'feat/send-evaluation-data-to-analytics-pipeline' of git…
Zaimwa9 0e819eb
feat: add trackEvent for custom pipeline events (#384)
talissoncosta 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import { getFlagsmith, environmentID, testIdentity } from './test-constants'; | ||
|
|
||
| const pipelineUrl = 'https://analytics.flagsmith.com/'; | ||
|
|
||
| function getPipelineCalls(mockFetch: jest.Mock) { | ||
| return mockFetch.mock.calls.filter( | ||
| ([url]: [string]) => url.includes('v1/analytics/batch') | ||
| ); | ||
| } | ||
|
|
||
| describe('Pipeline Analytics', () => { | ||
| test('should not send pipeline events when evaluationAnalyticsConfig is not set', async () => { | ||
| const { flagsmith, initConfig, mockFetch } = getFlagsmith(); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('hero'); | ||
| flagsmith.hasFeature('font_size'); | ||
|
|
||
| expect(getPipelineCalls(mockFetch)).toHaveLength(0); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should buffer events and flush with correct shape and headers', async () => { | ||
| const { flagsmith, initConfig, mockFetch } = getFlagsmith({ | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| flushInterval: 60000, | ||
| }, | ||
| }); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('font_size'); | ||
| flagsmith.hasFeature('hero'); | ||
|
|
||
| // @ts-ignore | ||
| await flagsmith.flushPipelineAnalytics(); | ||
|
|
||
| const calls = getPipelineCalls(mockFetch); | ||
| expect(calls).toHaveLength(1); | ||
|
|
||
| const body = JSON.parse(calls[0][1].body); | ||
| expect(body.environment_key).toBe(environmentID); | ||
| expect(body.events).toHaveLength(2); | ||
|
|
||
| const valueEvent = body.events[0]; | ||
| expect(valueEvent.event_id).toBe('font_size'); | ||
| expect(valueEvent.event_type).toBe('flag_evaluation'); | ||
| expect(valueEvent.value).toBe(16); | ||
| expect(valueEvent.enabled).toBe(true); | ||
| expect(valueEvent.identity_identifier).toBe(''); | ||
| expect(valueEvent.evaluated_at).toBeDefined(); | ||
| expect(valueEvent.metadata).toEqual(expect.objectContaining({ id: 6149 })); | ||
|
|
||
| const enabledEvent = body.events[1]; | ||
| expect(enabledEvent.event_id).toBe('hero'); | ||
| expect(enabledEvent.event_type).toBe('flag_evaluation'); | ||
| expect(enabledEvent.enabled).toBe(true); | ||
| expect(enabledEvent.value).toBe(flagsmith.getValue('hero')); | ||
|
|
||
| const headers = calls[0][1].headers; | ||
| expect(headers['X-Environment-Key']).toBe(environmentID); | ||
| expect(headers['Content-Type']).toBe('application/json; charset=utf-8'); | ||
| expect(headers['Flagsmith-SDK-User-Agent']).toMatch(/^flagsmith-js-sdk\//); | ||
| }); | ||
|
|
||
| test('should include identity and full traits when identified', async () => { | ||
| const { flagsmith, initConfig, mockFetch } = getFlagsmith({ | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| flushInterval: 60000, | ||
| }, | ||
| identity: testIdentity, | ||
| }); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('hero'); | ||
|
|
||
| // @ts-ignore | ||
| await flagsmith.flushPipelineAnalytics(); | ||
|
|
||
| const calls = getPipelineCalls(mockFetch); | ||
| const event = JSON.parse(calls[0][1].body).events[0]; | ||
|
|
||
| expect(event.identity_identifier).toBe(testIdentity); | ||
| expect(event.traits).toEqual({ | ||
| number_trait: { value: 1 }, | ||
| string_trait: { value: 'Example' }, | ||
| }); | ||
| }); | ||
|
|
||
| test('should cap buffer at maxBuffer and skip events when skipAnalytics is used', async () => { | ||
| const { flagsmith, initConfig } = getFlagsmith({ | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| maxBuffer: 3, | ||
| flushInterval: 60000, | ||
| }, | ||
| }); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('hero', { skipAnalytics: true }); | ||
| flagsmith.hasFeature('font_size', { skipAnalytics: true }); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(0); | ||
|
|
||
| flagsmith.getValue('hero'); | ||
| flagsmith.getValue('font_size'); | ||
| flagsmith.getValue('json_value'); | ||
| flagsmith.getValue('number_value'); | ||
| flagsmith.getValue('off_value'); | ||
|
|
||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(3); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents[0].event_id).toBe('json_value'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents[2].event_id).toBe('off_value'); | ||
| }); | ||
|
|
||
| test('should deduplicate repeated evaluations with same result per flush window', async () => { | ||
| const { flagsmith, initConfig, mockFetch } = getFlagsmith({ | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| flushInterval: 60000, | ||
| }, | ||
| identity: testIdentity, | ||
| }); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('font_size'); | ||
| flagsmith.getValue('font_size'); | ||
| flagsmith.getValue('font_size'); | ||
| flagsmith.hasFeature('font_size'); | ||
| flagsmith.hasFeature('font_size'); | ||
|
|
||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(1); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents[0].event_id).toBe('font_size'); | ||
|
|
||
| // @ts-ignore | ||
| await flagsmith.flushPipelineAnalytics(); | ||
| flagsmith.getValue('font_size'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(1); | ||
|
|
||
| flagsmith.getValue('hero'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(2); | ||
| }); | ||
|
|
||
| test('should record new event when evaluation result changes for same key', async () => { | ||
| const { flagsmith, initConfig } = getFlagsmith({ | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| flushInterval: 60000, | ||
| }, | ||
| }); | ||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('font_size'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(1); | ||
|
|
||
| flagsmith.getValue('font_size'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(1); | ||
|
|
||
| await flagsmith.identify(testIdentity); | ||
| flagsmith.getValue('font_size'); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(2); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents[1].identity_identifier).toBe(testIdentity); | ||
| }); | ||
|
|
||
| test('should re-queue on failure and coexist with standard analytics', async () => { | ||
| const { flagsmith, initConfig, mockFetch } = getFlagsmith({ | ||
| enableAnalytics: true, | ||
| evaluationAnalyticsConfig: { | ||
| analyticsServerUrl: pipelineUrl, | ||
| flushInterval: 60000, | ||
| }, | ||
| }); | ||
|
|
||
| const original = mockFetch.getMockImplementation() as jest.Mock; | ||
| mockFetch.mockImplementation(async (url: string, options: any) => { | ||
| if (url.includes('v1/analytics/batch')) { | ||
| return { status: 500, text: () => Promise.resolve('Server Error') }; | ||
| } | ||
| return original(url, options); | ||
| }); | ||
|
|
||
| await flagsmith.init(initConfig); | ||
|
|
||
| flagsmith.getValue('hero'); | ||
| flagsmith.getValue('font_size'); | ||
|
|
||
| // @ts-ignore | ||
| expect(flagsmith.evaluationEvent[environmentID]['hero']).toBe(1); | ||
| // @ts-ignore | ||
| expect(flagsmith.evaluationEvent[environmentID]['font_size']).toBe(1); | ||
|
|
||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(2); | ||
|
|
||
| // @ts-ignore | ||
| await flagsmith.flushPipelineAnalytics(); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents).toHaveLength(2); | ||
| // @ts-ignore | ||
| expect(flagsmith.pipelineEvents[0].event_id).toBe('hero'); | ||
| }); | ||
| }); | ||
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.