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); } }