Skip to content

Commit

Permalink
refactor: inline encode helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Oct 13, 2018
1 parent 4688c11 commit dc003bc
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
var KEY = 'ga:user';
var UID = (localStorage[KEY] = localStorage[KEY] || Math.random() + '.' + Math.random());

// modified `obj-str`
function encode(obj) {
var k, str='https://www.google-analytics.com/collect?v=1';
for (k in obj) {
if (obj[k]) {
str += ('&' + k + '=' + encodeURIComponent(obj[k]));
}
}
return str;
}

function GA(ua, opts, toWait) {
opts = opts || {};
this.args = Object.assign({ tid:ua, cid:UID }, opts);
Expand All @@ -23,7 +12,12 @@ GA.prototype.send = function (type, opts) {
opts = { dl:location.href, dt:document.title };
}
var obj = Object.assign({ t:type }, this.args, opts, { z:Date.now() });
new Image().src = encode(obj); // dispatch a GET
}
var k, str='https://www.google-analytics.com/collect?v=1';
for (k in obj) {
// modified `obj-str` behavior
if (obj[k]) str += ('&' + k + '=' + encodeURIComponent(obj[k]));
}
new Image().src = str; // dispatch a GET
};

export default GA;

0 comments on commit dc003bc

Please sign in to comment.