From 79f6df1f36bbb6f738dbcde3a961b79de0d54582 Mon Sep 17 00:00:00 2001 From: Han Kim Date: Wed, 6 Jul 2016 13:45:20 -0700 Subject: [PATCH] push userId to data layer for track calls --- lib/index.js | 3 +++ test/index.test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/index.js b/lib/index.js index fb6daf9..4059b24 100644 --- a/lib/index.js +++ b/lib/index.js @@ -83,6 +83,9 @@ GTM.prototype.page = function(page) { GTM.prototype.track = function(track) { var props = track.properties(); + var id = this.analytics.user().id(); + if (id) props.userId = id; props.event = track.event(); + push(props); }; diff --git a/test/index.test.js b/test/index.test.js index 81aba73..b55e34e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -67,6 +67,12 @@ describe('Google Tag Manager', function() { analytics.called(window.dataLayer.push, { event: 'some-event' }); }); + it('should send userId if it exists', function() { + analytics.user().id('pablo'); + analytics.track('some-event'); + analytics.called(window.dataLayer.push, { userId: 'pablo', event: 'some-event' }); + }); + it('should send event with properties', function() { analytics.track('event', { prop: true }); analytics.called(window.dataLayer.push, { event: 'event', prop: true });