From a5f3eb318990b370951f6945401154ab0a72e071 Mon Sep 17 00:00:00 2001 From: Jody Brewster Date: Fri, 24 Jun 2016 15:47:42 -0400 Subject: [PATCH] Update Analytics.js Added ability to set user ID --- lib/Analytics.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Analytics.js b/lib/Analytics.js index 8541ba4..fc034d1 100644 --- a/lib/Analytics.js +++ b/lib/Analytics.js @@ -6,6 +6,7 @@ export default class Analytics { this.trackingId = trackingId; this.clientId = clientId; this.userAgent = userAgent || null; + this.userId = null; this.customDimensions = new CustomDimensions(); if (!userAgent) { @@ -15,18 +16,24 @@ export default class Analytics { addDimension(index, name) { this.customDimensions.addDimension(index, name); + } removeDimension(index) { this.customDimensions.removeDimension(index); } + setUserId(userId) { + this.userId = userId; + } + send(hit) { + let uidstr = ''; hit.set({ v: this.version, tid: this.trackingId, - cid: this.clientId + cid: this.clientId, }); let options = { @@ -36,6 +43,9 @@ export default class Analytics { } } - return fetch(`https://www.google-analytics.com/collect?${hit.toQueryString()}&${this.customDimensions.toQueryString()}&z=${Math.round(Math.random() * 1e8)}`, options); + if (this.userId !== null && this.userId !== undefined) { + uidstr = `uid=${this.userId}&`; + } + return fetch(`https://www.google-analytics.com/collect?${uidstr}${hit.toQueryString()}&${this.customDimensions.toQueryString()}&z=${Math.round(Math.random() * 1e8)}`, options); } }