Skip to content

Commit

Permalink
feat: Add google global site tag (gtag) adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
44px committed Nov 5, 2018
1 parent daee27f commit 882507f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/adapters/google-ga.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { googleGA } from './google-ga';
import { GA, googleGA } from './google-ga';

declare global {
namespace NodeJS {
interface Global {
ga?: Function;
ga?: GA;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/google-ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { EventProperties, EventTracker } from '../wrapped-analytics';
// ga function signature:
// ga('send', 'event', [eventCategory], [eventAction], [eventLabel], [eventValue], [fieldsObject]);
// see https://developers.google.com/analytics/devguides/collection/analyticsjs/events#implementation
interface GA {
export interface GA {
(
method: string,
type: string,
eventCategory?: string,
eventAction?: string,
eventLabel?: string,
eventValue?: string,
fieldsObject?: any,
): void;
}
Expand Down
45 changes: 45 additions & 0 deletions src/adapters/google-gtag.test.ts
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',
});
});
});
43 changes: 43 additions & 0 deletions src/adapters/google-gtag.ts
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,
});
};

0 comments on commit 882507f

Please sign in to comment.