Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 7490ebf

Browse files
author
Bryan Mikaelian
committed
Replace utils/each with Array.prototype.forEach in analytics.ts
Also removes @ndhoule/keys
1 parent 4961414 commit 7490ebf

File tree

5 files changed

+108
-45
lines changed

5 files changed

+108
-45
lines changed

lib/analytics.ts

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ function Analytics() {
7070
this.log = debug('analytics.js');
7171
bindAll(this);
7272

73-
var self = this;
74-
this.on('initialize', function(settings, options) {
73+
74+
const self = this;
75+
this.on('initialize', function(_, options) {
7576
if (options.initialPageview) self.page();
7677
self._parseQuery(window.location.search);
7778
});
@@ -170,13 +171,16 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
170171

171172
// clean unknown integrations from settings
172173
var self = this;
173-
each(function(_opts: unknown, name: string | number) {
174-
var Integration = self.Integrations[name];
175-
if (!Integration) delete settings[name];
176-
}, settings);
174+
Object.keys(settings).forEach(key => {
175+
var Integration = self.Integrations[key];
176+
if (!Integration) delete settings[key];
177+
});
177178

178179
// add integrations
179-
each(function(opts: unknown, name: string | number) {
180+
Object.keys(settings).forEach(key => {
181+
const opts = settings[key]
182+
const name = key
183+
180184
// Don't load disabled integrations
181185
if (options.integrations) {
182186
if (
@@ -187,13 +191,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
187191
}
188192
}
189193

190-
var Integration = self.Integrations[name];
191-
var clonedOpts = {};
194+
const Integration = self.Integrations[name];
195+
const clonedOpts = {};
192196
extend(true, clonedOpts, opts); // deep clone opts
193-
var integration = new Integration(clonedOpts);
197+
const integration = new Integration(clonedOpts);
194198
self.log('initialize %o - %o', name, opts);
195199
self.add(integration);
196-
}, settings);
200+
});
197201

198202
var integrations = this._integrations;
199203

@@ -220,14 +224,15 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
220224
// initialize integrations, passing ready
221225
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
222226
this.failedInitializations = [];
223-
var initialPageSkipped = false;
224-
each(function(integration) {
227+
let initialPageSkipped = false;
228+
Object.keys(integrations).forEach(key => {
229+
const integration = integrations[key]
225230
if (
226231
options.initialPageview &&
227232
integration.options.initialPageview === false
228233
) {
229234
// We've assumed one initial pageview, so make sure we don't count the first page call.
230-
var page = integration.page;
235+
let page = integration.page;
231236
integration.page = function() {
232237
if (initialPageSkipped) {
233238
return page.apply(this, arguments);
@@ -247,7 +252,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
247252
});
248253
integration.initialize();
249254
} catch (e) {
250-
var integrationName = integration.name;
255+
let integrationName = integration.name;
251256
metrics.increment('analytics_js.integration.invoke.error', {
252257
method: 'initialize',
253258
integration_name: integration.name
@@ -258,7 +263,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
258263

259264
integration.ready();
260265
}
261-
}, integrations);
266+
});
262267

263268
// backwards compat with angular plugin and used for init logic checks
264269
this.initialized = true;
@@ -467,37 +472,44 @@ Analytics.prototype.track = function(
467472
*/
468473

469474
Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(
470-
links: Element | Array<unknown>,
475+
links: Element | Array<Element> | JQuery,
471476
event: any,
472477
properties?: any
473478
): SegmentAnalytics {
479+
let elements: Array<Element> = []
474480
if (!links) return this;
475481
// always arrays, handles jquery
476-
if (type(links) === 'element') links = [links];
482+
if (links instanceof Element) {
483+
elements = [links]
484+
} else if ("toArray" in links) {
485+
elements = links.toArray()
486+
} else {
487+
elements = links as Array<Element>
488+
}
477489

478-
var self = this;
479-
each(function(el) {
490+
elements.forEach(el => {
480491
if (type(el) !== 'element') {
481492
throw new TypeError('Must pass HTMLElement to `analytics.trackLink`.');
482493
}
483-
on(el, 'click', function(e) {
484-
var ev = is.fn(event) ? event(el) : event;
485-
var props = is.fn(properties) ? properties(el) : properties;
486-
var href =
494+
on(el, 'click', (e) => {
495+
const ev = is.fn(event) ? event(el) : event;
496+
const props = is.fn(properties) ? properties(el) : properties;
497+
const href =
487498
el.getAttribute('href') ||
488499
el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') ||
489500
el.getAttribute('xlink:href');
490501

491-
self.track(ev, props);
502+
this.track(ev, props);
492503

504+
// @ts-ignore
493505
if (href && el.target !== '_blank' && !isMeta(e)) {
494506
prevent(e);
495-
self._callback(function() {
507+
this._callback(function() {
496508
window.location.href = href;
497509
});
498510
}
499511
});
500-
}, links);
512+
});
501513

502514
return this;
503515
};
@@ -523,18 +535,19 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
523535
// always arrays, handles jquery
524536
if (type(forms) === 'element') forms = [forms];
525537

526-
var self = this;
527-
each(function(el: { submit: () => void }) {
538+
const elements = forms as Array<unknown>
539+
540+
elements.forEach((el: { submit: () => void }) => {
528541
if (type(el) !== 'element')
529542
throw new TypeError('Must pass HTMLElement to `analytics.trackForm`.');
530-
function handler(e) {
543+
const handler = (e) => {
531544
prevent(e);
532545

533-
var ev = is.fn(event) ? event(el) : event;
534-
var props = is.fn(properties) ? properties(el) : properties;
535-
self.track(ev, props);
546+
const ev = is.fn(event) ? event(el) : event;
547+
const props = is.fn(properties) ? properties(el) : properties;
548+
this.track(ev, props);
536549

537-
self._callback(function() {
550+
this._callback(function() {
538551
el.submit();
539552
});
540553
}
@@ -547,7 +560,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
547560
} else {
548561
on(el, 'submit', handler);
549562
}
550-
}, forms);
563+
});
551564

552565
return this;
553566
};
@@ -565,11 +578,11 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
565578
*/
566579

567580
Analytics.prototype.page = function(
568-
category: string,
569-
name: string,
570-
properties: any,
571-
options: any,
572-
fn: unknown
581+
category?: string,
582+
name?: string,
583+
properties?: any,
584+
options?: any,
585+
fn?: unknown
573586
): SegmentAnalytics {
574587
// Argument reshuffling.
575588
/* eslint-disable no-unused-expressions, no-sequences */
@@ -797,9 +810,11 @@ Analytics.prototype._invoke = function(
797810
return this;
798811

799812
function applyIntegrationMiddlewares(facade) {
800-
var failedInitializations = self.failedInitializations || [];
801-
each(function(integration, name) {
802-
var facadeCopy = extend(true, new Facade({}), facade);
813+
let failedInitializations = self.failedInitializations || [];
814+
Object.keys(self._integrations).forEach(key => {
815+
const integration = self._integrations[key]
816+
const { name } = integration
817+
const facadeCopy = extend(true, new Facade({}), facade);
803818

804819
if (!facadeCopy.enabled(name)) return;
805820
// Check if an integration failed to initialize.
@@ -883,7 +898,7 @@ Analytics.prototype._invoke = function(
883898
);
884899
}
885900
}
886-
}, self._integrations);
901+
});
887902
}
888903
};
889904

lib/types.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
export interface SegmentAnalytics {
2-
Integrations: { [name: string]: unknown };
2+
Integrations: { [name: string]: (options: SegmentOpts) => void };
33
options: InitOptions;
44
require: any
55
VERSION: any
6+
7+
// Analytics.JS Methods
8+
page: (
9+
category?: string,
10+
name?: string,
11+
properties?: any,
12+
options?: any,
13+
fn?: unknown
14+
) => void
15+
16+
// Private fields
17+
_options: (options: Object) => void
18+
_sourceMiddlewares: unknown
19+
_integrationMiddlewares: unknown
20+
_destinationMiddlewares: unknown
21+
_integrations: unknown
22+
_readied: boolean
23+
_timeout: number
24+
_user: unknown
25+
log: (args: string) => void
26+
on: (event: string, callback: (settings: unknown, options: InitOptions) => void) => void
27+
_parseQuery: (queryString: string) => void
628
}
729

830
export interface IntegrationsSettings {

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@
6767
"@codeceptjs/mock-request": "^0.3.0",
6868
"@segment/analytics.js-integration": "^3.2.2",
6969
"@segment/eslint-config": "^4.0.0",
70+
"@types/component-emitter": "^1.2.9",
71+
"@types/debug": "^4.1.5",
7072
"@types/express": "^4.17.6",
73+
"@types/jquery": "^3.5.1",
7174
"@types/lodash": "^4.14.155",
7275
"@types/mocha": "^7.0.2",
7376
"@types/node": "^14.0.6",

test/analytics.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var bind = require('component-event').bind;
88
var createIntegration = require('@segment/analytics.js-integration');
99
var extend = require('@ndhoule/extend');
1010
var type = require('component-type');
11+
// @ts-ignore
1112
var jQuery = require('jquery');
1213
var pageDefaults = require('../build/pageDefaults');
1314
var sinon = require('sinon');

yarn.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,23 @@
517517
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
518518
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
519519

520+
"@types/component-emitter@^1.2.9":
521+
version "1.2.9"
522+
resolved "https://registry.yarnpkg.com/@types/component-emitter/-/component-emitter-1.2.9.tgz#a17785a0fa86d934ac8b762bd496c65ff9e47c84"
523+
integrity sha512-xaESaDSXQLIGYpXJJ+ubBsDrsbI07Wv9zzDa7qb1RFnQUTeBZjZH9QfEaRn1AkvPdVK2d66vl4sR001vK+lR+g==
524+
520525
"@types/connect@*":
521526
version "3.4.33"
522527
resolved "https://registry.yarnpkg.com/@types/connect/-/connect-3.4.33.tgz#31610c901eca573b8713c3330abc6e6b9f588546"
523528
integrity sha512-2+FrkXY4zllzTNfJth7jOqEHC+enpLeGslEhpnTAkg21GkRrWV4SsAtqchtT4YS9/nODBU2/ZfsBY2X4J/dX7A==
524529
dependencies:
525530
"@types/node" "*"
526531

532+
"@types/debug@^4.1.5":
533+
version "4.1.5"
534+
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd"
535+
integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ==
536+
527537
"@types/express-serve-static-core@*":
528538
version "4.17.7"
529539
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.7.tgz#dfe61f870eb549dc6d7e12050901847c7d7e915b"
@@ -543,6 +553,13 @@
543553
"@types/qs" "*"
544554
"@types/serve-static" "*"
545555

556+
"@types/jquery@^3.5.1":
557+
version "3.5.1"
558+
resolved "https://registry.yarnpkg.com/@types/jquery/-/jquery-3.5.1.tgz#cebb057acf5071c40e439f30e840c57a30d406c3"
559+
integrity sha512-Tyctjh56U7eX2b9udu3wG853ASYP0uagChJcQJXLUXEU6C/JiW5qt5dl8ao01VRj1i5pgXPAf8f1mq4+FDLRQg==
560+
dependencies:
561+
"@types/sizzle" "*"
562+
546563
"@types/lodash@^4.14.155":
547564
version "4.14.155"
548565
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.155.tgz#e2b4514f46a261fd11542e47519c20ebce7bc23a"
@@ -609,6 +626,11 @@
609626
"@types/express-serve-static-core" "*"
610627
"@types/mime" "*"
611628

629+
"@types/sizzle@*":
630+
version "2.3.2"
631+
resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.2.tgz#a811b8c18e2babab7d542b3365887ae2e4d9de47"
632+
integrity sha512-7EJYyKTL7tFR8+gDbB6Wwz/arpGa0Mywk1TJbNzKzHtzbwVmY4HR9WqS5VV7dsBUKQmPNr192jHr/VpBluj/hg==
633+
612634
"@types/tough-cookie@*":
613635
version "4.0.0"
614636
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.0.tgz#fef1904e4668b6e5ecee60c52cc6a078ffa6697d"

0 commit comments

Comments
 (0)