-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analytics events helper in JS Runtime (#315)
- Loading branch information
Showing
5 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/runtimes/js/src/helpers/browser/analytics/analytics.d.ts
This file contains 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,6 @@ | ||
export type AtlaspackAnalyticsEvent = { | ||
action: string; | ||
attributes?: {[key: string]: string | number | boolean}; | ||
}; | ||
|
||
export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void; |
6 changes: 6 additions & 0 deletions
6
packages/runtimes/js/src/helpers/browser/analytics/analytics.js
This file contains 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,6 @@ | ||
function sendAnalyticsEvent(detail) { | ||
const ev = new globalThis.CustomEvent('atlaspack:analytics', {detail}); | ||
globalThis.dispatchEvent(ev); | ||
} | ||
|
||
module.exports = {sendAnalyticsEvent}; |
8 changes: 8 additions & 0 deletions
8
packages/runtimes/js/src/helpers/browser/analytics/analytics.js.flow
This file contains 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,8 @@ | ||
// @flow | ||
|
||
export type AtlaspackAnalyticsEvent = {| | ||
action: string, | ||
attributes?: { [key: string]: string | number | boolean }, | ||
|}; | ||
|
||
declare export function sendAnalyticsEvent(event: AtlaspackAnalyticsEvent): void; |
This file contains 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 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,47 @@ | ||
import {sendAnalyticsEvent} from '../src/helpers/browser/analytics/analytics.js'; | ||
import {mock} from 'node:test'; | ||
import type {Mock} from 'node:test'; | ||
import assert from 'node:assert'; | ||
|
||
describe('@atlaspack/analytics', () => { | ||
let dispatchEventMock: Mock<Window['dispatchEvent']>; | ||
|
||
beforeEach(() => { | ||
// @ts-expect-error | ||
globalThis.CustomEvent = MockCustomEvent; | ||
dispatchEventMock = mock.fn(); | ||
globalThis.dispatchEvent = dispatchEventMock; | ||
}); | ||
|
||
it('should not throw', () => { | ||
assert.doesNotThrow(() => | ||
sendAnalyticsEvent({ | ||
action: 'test', | ||
}), | ||
); | ||
}); | ||
|
||
it('should raise event on window', () => { | ||
sendAnalyticsEvent({ | ||
action: 'test', | ||
}); | ||
|
||
assert.equal(dispatchEventMock.mock.callCount(), 1); | ||
assert.deepEqual(dispatchEventMock.mock.calls[0].arguments[0], { | ||
eventName: 'atlaspack:analytics', | ||
detail: { | ||
action: 'test', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
class MockCustomEvent { | ||
eventName: string; | ||
detail: any; | ||
|
||
constructor(eventName: string, options: any = {}) { | ||
this.eventName = eventName; | ||
this.detail = options.detail; | ||
} | ||
} |