Skip to content

Commit

Permalink
Update GA (gtag.js) to work with page view event-helper (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttmarek authored Apr 15, 2018
1 parent cbcf784 commit df51419
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/google-analytics-gtag/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redux-beacon/google-analytics-gtag",
"version": "1.0.0",
"version": "1.0.1",
"description": "Google Analytics (gtag.js) integration for Redux",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ describe('Page Tracking', () => {
expect(window.gtag).toHaveBeenCalledWith('config', 'GA_TRACKING_ID_1', {});
expect(window.gtag).toHaveBeenCalledWith('config', 'GA_TRACKING_ID_2', {});
});

test('given { type: "page", trackingId: [] } (empty trackPageView)', () => {
const events = [
{
type: 'page',
trackingId: [],
page_path: '/topics',
},
];

const target = GoogleAnalyticsGtag('GA_TRACKING_ID');

target(events);

expect(window.gtag).toHaveBeenCalledWith('config', 'GA_TRACKING_ID', {
page_path: '/topics',
});
});
});

describe('Event Tracking', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/google-analytics-gtag/src/google-analytics-gtag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Target } from 'redux-beacon';

declare let gtag: any;

function GoogleAnalyticsGtag(gaTrackingId: string): Target {
function GoogleAnalyticsGtag(defaultTrackingId: string): Target {
if (typeof window === 'undefined') {
return () => {};
}
Expand All @@ -13,7 +13,7 @@ function GoogleAnalyticsGtag(gaTrackingId: string): Target {
);
}

gtag('config', gaTrackingId, { send_page_view: false });
gtag('config', defaultTrackingId, { send_page_view: false });

return function target(events) {
const pageTracking = events.filter(event => event.type === 'page');
Expand All @@ -22,13 +22,13 @@ function GoogleAnalyticsGtag(gaTrackingId: string): Target {
pageTracking.forEach(event => {
const { type, trackingId, ...params } = event;

let trackingIds = [gaTrackingId];
let trackingIds = [defaultTrackingId];

if (typeof trackingId === 'string') {
trackingIds = [trackingId];
}

if (Array.isArray(trackingId)) {
if (Array.isArray(trackingId) && trackingId.length > 0) {
trackingIds = trackingId;
}

Expand Down

0 comments on commit df51419

Please sign in to comment.