From 2be05a1fabc33c17c7587da04378b16e4e5f5677 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 19 Jul 2022 15:06:06 -0700 Subject: [PATCH 1/5] Code golf with modern syntax & features --- src/index.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 8ab9257..13a05ce 100644 --- a/src/index.js +++ b/src/index.js @@ -1,23 +1,21 @@ -var KEY = 'ga:user'; +let KEY = 'ga:user'; -export default function (ua, args, toWait) { - args = Object.assign({}, args, { +export default (ua, args, toWait) => { + args = { + ...args, tid: ua, - cid: (localStorage[KEY] = localStorage[KEY] || Math.random() + '.' + Math.random()) - }); + cid: localStorage[KEY] ??= Math.random() + '.' + Math.random() + }; - function send(type, opts) { - if (type === 'pageview' && !opts) { - opts = { dl:location.href, dt:document.title }; - } - var k, str='https://www.google-analytics.com/collect?v=1'; - var obj = Object.assign({ t:type }, args, opts, { z:Date.now() }); - for (k in obj) { - // modified `obj-str` behavior - if (obj[k]) str += ('&' + k + '=' + encodeURIComponent(obj[k])); - } - new Image().src = str; // dispatch a GET - } + let send = (type, opts = type === 'pageview' && { dl:location.href, dt:document.title }) => { + fetch(`https://www.google-analytics.com/collect?${new URLSearchParams({ + t: type, + ...args, + ...opts, + z: +Date(), + v: 1, + })}`); + }; toWait || send('pageview'); From 80e03632df7651016e17ec54dd9fae307a8ccde1 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 19 Jul 2022 15:11:27 -0700 Subject: [PATCH 2/5] Remove unnecessary KEY var --- src/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 13a05ce..91493df 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,8 @@ -let KEY = 'ga:user'; - export default (ua, args, toWait) => { args = { ...args, tid: ua, - cid: localStorage[KEY] ??= Math.random() + '.' + Math.random() + cid: localStorage['ga:user'] ??= Math.random() + '.' + Math.random() }; let send = (type, opts = type === 'pageview' && { dl:location.href, dt:document.title }) => { From 1a88e25c3f37602e93151c5c93a76c06d981f545 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 19 Jul 2022 15:13:12 -0700 Subject: [PATCH 3/5] String concat is shorter than template string --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 91493df..67d265c 100644 --- a/src/index.js +++ b/src/index.js @@ -6,13 +6,13 @@ export default (ua, args, toWait) => { }; let send = (type, opts = type === 'pageview' && { dl:location.href, dt:document.title }) => { - fetch(`https://www.google-analytics.com/collect?${new URLSearchParams({ + fetch('https://www.google-analytics.com/collect?' + new URLSearchParams({ t: type, ...args, ...opts, z: +Date(), v: 1, - })}`); + })); }; toWait || send('pageview'); From f3f4634f64550fcefd593e9ba231b8627139ec7a Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 19 Jul 2022 22:21:34 +0000 Subject: [PATCH 4/5] bump dependencies --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 6db3d21..c4d2afc 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,8 @@ "ganalytics" ], "devDependencies": { - "bundt": "^0.4.0", + "bundt": "^1.1.5", "tap-spec": "^5.0.0", - "tape": "^4.9.1" + "tape": "^5.5.3" } } From 7041dbbb20040044089ceaf1208fa9f04c49daa6 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 19 Jul 2022 22:24:07 +0000 Subject: [PATCH 5/5] Keep export working as constructor --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 67d265c..b2e781c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -export default (ua, args, toWait) => { +export default function (ua, args, toWait) { args = { ...args, tid: ua,