-
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.
feat: Add google global site tag (gtag) adapter
- Loading branch information
Showing
4 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
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
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,45 @@ | ||
import { googleGTAG, GTAG } from './google-gtag'; | ||
|
||
declare global { | ||
namespace NodeJS { | ||
interface Global { | ||
gtag?: GTAG; | ||
} | ||
} | ||
} | ||
|
||
describe('googleGTAG', () => { | ||
it('does nothing if Google Global Site Tag function not found', () => { | ||
expect(() => { | ||
googleGTAG('test'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('maps event only', () => { | ||
global.gtag = jest.fn(); | ||
|
||
googleGTAG('testEvent'); | ||
|
||
expect(global.gtag).toBeCalledWith('event', 'testEvent', { | ||
event_category: undefined, | ||
event_label: undefined, | ||
value: undefined, | ||
}); | ||
}); | ||
|
||
it('maps event and its properties', () => { | ||
global.gtag = jest.fn(); | ||
|
||
googleGTAG('testEvent', { | ||
category: 'testCategory', | ||
label: 'testLabel', | ||
value: 'testValue', | ||
}); | ||
|
||
expect(global.gtag).toBeCalledWith('event', 'testEvent', { | ||
event_category: 'testCategory', | ||
event_label: 'testLabel', | ||
value: 'testValue', | ||
}); | ||
}); | ||
}); |
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,43 @@ | ||
import { EventProperties, EventTracker } from '../wrapped-analytics'; | ||
|
||
// gtag function signature: | ||
// gtag('event', <action>, { | ||
// 'event_category': <category>, | ||
// 'event_label': <label>, | ||
// 'value': <value> | ||
// }); | ||
// see https://developers.google.com/analytics/devguides/collection/gtagjs/events#send_events | ||
export interface GTAG { | ||
( | ||
type: string, | ||
eventAction: string, | ||
eventProperties?: { | ||
event_category?: string; | ||
event_label?: string; | ||
value?: string; | ||
}, | ||
): void; | ||
} | ||
|
||
declare global { | ||
interface Window { | ||
gtag?: GTAG; | ||
} | ||
} | ||
|
||
export const googleGTAG: EventTracker = ( | ||
event: string, | ||
eventProperties?: EventProperties, | ||
): void => { | ||
const gtag = window.gtag; | ||
if (!gtag) { | ||
return; | ||
} | ||
|
||
const { category, label, value } = eventProperties || ({} as EventProperties); | ||
gtag('event', event, { | ||
event_category: category, | ||
event_label: label, | ||
value: value, | ||
}); | ||
}; |