Skip to content
This repository has been archived by the owner on May 15, 2020. It is now read-only.

Update Analytics.js #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/Analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 = {
Expand All @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could just be a falsy check, no? if (this.userId) { ? -- Also I'd probably prefer just uid as the variable name.

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