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

Commit d4d70e3

Browse files
Drop @segment/canonical. Also uprade some libs (#213)
1 parent 7b59f34 commit d4d70e3

9 files changed

+112
-45
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 4.1.5 / 2020-09-20
2+
3+
- Remove `@segment/canonical` in favor of `document.querySelector`
4+
15
# 4.1.5 / 2020-09-17
26

37
- Replace @ndhoule/defaults with merging via ES6 spread syntax

karma.conf.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = function(config) {
4444
module: "commonjs",
4545
target: "ES5",
4646
allowJs: false,
47+
esModuleInterop: true
4748
},
4849
include: ['test'],
4950
exclude: ['node_modules', 'lib', 'test-e2e/*.ts']

lib/analytics.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
PageDefaults, Message
1010
} from './types';
1111

12+
import { pageDefaults } from './pageDefaults';
13+
1214
import cloneDeep from 'lodash.clonedeep'
1315
import pick from 'lodash.pick'
1416

@@ -43,7 +45,6 @@ var memory = require('./memory');
4345
var nextTick = require('next-tick');
4446
var normalize = require('./normalize');
4547
var on = require('component-event').bind;
46-
var pageDefaults = require('./pageDefaults');
4748
var prevent = require('@segment/prevent-default');
4849
var querystring = require('component-querystring');
4950
var store = require('./store');

lib/pageDefaults.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
11
import { PageDefaults } from './types';
22
import includes from 'lodash.includes'
3-
import canonical from '@segment/canonical'
43
import url from 'component-url'
54

6-
/**
7-
* Return a default `options.context.page` object.
8-
*
9-
* https://segment.com/docs/spec/page/#properties
10-
*/
11-
12-
function pageDefaults(): PageDefaults {
13-
return {
14-
path: canonicalPath(),
15-
referrer: document.referrer,
16-
search: location.search,
17-
title: document.title,
18-
url: canonicalUrl(location.search)
19-
};
20-
}
21-
225
/**
236
* Return the canonical path for the page.
247
*/
258

26-
function canonicalPath(): string {
27-
const canon = canonical();
9+
const canonicalPath = (): string => {
10+
const canon = document.querySelector("link[rel='canonical']")
2811
if (!canon) return window.location.pathname;
29-
const parsed = url.parse(canon);
12+
const href = canon.getAttribute("href")
13+
14+
const parsed = url.parse(href);
3015
return parsed.pathname;
3116
}
3217

@@ -35,16 +20,35 @@ function canonicalPath(): string {
3520
* and strip the hash.
3621
*/
3722

38-
function canonicalUrl(search: string): string {
39-
const canon = canonical();
40-
if (canon) return includes(canon, '?') ? canon : canon + search;
23+
const canonicalUrl = (search: string): string => {
24+
const canon = document.querySelector("link[rel='canonical']")
25+
if (canon) {
26+
const href = canon.getAttribute("href")
27+
return includes(href, '?') ? href : href + search;
28+
}
29+
4130
const url = window.location.href;
4231
const i = url.indexOf('#');
4332
return i === -1 ? url : url.slice(0, i);
4433
}
4534

46-
/*
47-
* Exports.
35+
/**
36+
* Return a default `options.context.page` object.
37+
*
38+
* https://segment.com/docs/spec/page/#properties
4839
*/
4940

50-
module.exports = pageDefaults;
41+
export const pageDefaults = (): PageDefaults => {
42+
const path = canonicalPath()
43+
const { referrer, title } = document
44+
const { search } = location
45+
const url = canonicalUrl(search)
46+
47+
return {
48+
path,
49+
referrer,
50+
search,
51+
title,
52+
url
53+
};
54+
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-core",
33
"author": "Segment <[email protected]>",
4-
"version": "4.1.5",
4+
"version": "4.1.6",
55
"description": "The hassle-free way to integrate analytics into any web application.",
66
"types": "lib/index.d.ts",
77
"keywords": [
@@ -30,7 +30,6 @@
3030
},
3131
"homepage": "https://github.com/segmentio/analytics.js-core#readme",
3232
"dependencies": {
33-
"@segment/canonical": "^1.0.0",
3433
"@segment/cookie": "^1.1.5",
3534
"@segment/is-meta": "^1.0.0",
3635
"@segment/isodate": "^1.0.2",
@@ -73,6 +72,8 @@
7372
"@types/mocha": "^7.0.2",
7473
"@types/node": "^14.0.6",
7574
"@types/node-fetch": "^2.5.7",
75+
"@types/proclaim": "^3.6.1",
76+
"@types/sinon": "^9.0.5",
7677
"@typescript-eslint/eslint-plugin": "^4.1.0",
7778
"@typescript-eslint/parser": "^4.1.0",
7879
"assert": "1.5.0",
@@ -109,7 +110,7 @@
109110
"prettier-eslint-cli": "5.0.0",
110111
"proclaim": "^3.5.1",
111112
"puppeteer": "^5.3.0",
112-
"sinon": "^1.7.3",
113+
"sinon": "^1.17.7",
113114
"snyk": "^1.393.0",
114115
"ts-node": "^8.10.2",
115116
"typescript": "^4.0.2",

test/analytics.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import assignIn from 'lodash.assignin'
2+
import { pageDefaults} from '../build/pageDefaults';
3+
import assert from 'proclaim'
24

35
var Analytics = require('../build').constructor;
46
var Facade = require('segmentio-facade');
57
var analytics = require('../build');
6-
var assert = require('proclaim');
78
var bind = require('component-event').bind;
89
var createIntegration = require('@segment/analytics.js-integration');
910
var type = require('component-type');
10-
var pageDefaults = require('../build/pageDefaults');
1111
var sinon = require('sinon');
1212
var tick = require('next-tick');
1313
var trigger = require('compat-trigger-event');

test/pageDefaults.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { pageDefaults } from '../build/pageDefaults'
2+
import * as assert from 'proclaim'
3+
import sinon from 'sinon'
4+
5+
const el = document.createElement("link")
6+
el.setAttribute("rel", "canonical")
7+
8+
const loc = window.location
9+
10+
describe('pageDefaults', () => {
11+
before(() => {
12+
el.setAttribute("href", "")
13+
sinon.stub(document, 'querySelector').returns(el)
14+
})
15+
16+
after(() => {
17+
sinon.restore()
18+
})
19+
20+
it('handles no canonical links', () => {
21+
const defs = pageDefaults()
22+
assert.isNotNull(defs.url)
23+
})
24+
25+
it('handles canonical links', () => {
26+
el.setAttribute("href", "http://www.segment.local")
27+
const defs = pageDefaults()
28+
assert.equal(defs.url, "http://www.segment.local")
29+
})
30+
31+
it('handles canonical links with a path', () => {
32+
el.setAttribute("href", "http://www.segment.local/test")
33+
const defs = pageDefaults()
34+
assert.equal(defs.url, "http://www.segment.local/test")
35+
assert.equal(defs.path, "/test")
36+
})
37+
38+
it('handles canonical links with search params in the url', () => {
39+
el.setAttribute("href", "http://www.segment.local?test=true")
40+
const defs = pageDefaults()
41+
assert.equal(defs.url, "http://www.segment.local?test=true")
42+
})
43+
})

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"module": "commonjs",
55
"target": "ES5",
66
"allowJs": true,
7-
"outDir": "build"
7+
"outDir": "build",
8+
"lib": ["dom"]
89
},
910
"include": ["lib"],
1011
"exclude": ["node_modules", "*.md", "Makefile", "karma.*", "test", "test-e2e"]

yarn.lock

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,6 @@
449449
dependencies:
450450
utf8-encode "1"
451451

452-
"@segment/canonical@^1.0.0":
453-
version "1.0.0"
454-
resolved "https://registry.yarnpkg.com/@segment/canonical/-/canonical-1.0.0.tgz#9adb1a731d29ab975493bf17a4ce4952985c4920"
455-
456452
"@segment/cookie@^1.1.5":
457453
version "1.1.5"
458454
resolved "https://registry.yarnpkg.com/@segment/cookie/-/cookie-1.1.5.tgz#cbaaf26c73a8e34103312b5bd61ebbdb710611a8"
@@ -956,6 +952,11 @@
956952
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
957953
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==
958954

955+
"@types/proclaim@^3.6.1":
956+
version "3.6.1"
957+
resolved "https://registry.yarnpkg.com/@types/proclaim/-/proclaim-3.6.1.tgz#edfdf8e7b47f5996faee2313473ddb1b529847e7"
958+
integrity sha512-xWRRXajUTxbh1wDSK93lb1MICrbfaD2bGtT5cgBNFmY91g1GAAhQQ7pztnkx7BSbZvf8PxfGBsMOZ9311lDrVw==
959+
959960
"@types/qs@*":
960961
version "6.9.3"
961962
resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.3.tgz#b755a0934564a200d3efdf88546ec93c369abd03"
@@ -996,6 +997,18 @@
996997
"@types/express-serve-static-core" "*"
997998
"@types/mime" "*"
998999

1000+
"@types/sinon@^9.0.5":
1001+
version "9.0.5"
1002+
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-9.0.5.tgz#56b2a12662dd8c7d081cdc511af5f872cb37377f"
1003+
integrity sha512-4CnkGdM/5/FXDGqL32JQ1ttVrGvhOoesLLF7VnTh4KdjK5N5VQOtxaylFqqTjnHx55MnD9O02Nbk5c1ELC8wlQ==
1004+
dependencies:
1005+
"@types/sinonjs__fake-timers" "*"
1006+
1007+
"@types/sinonjs__fake-timers@*":
1008+
version "6.0.1"
1009+
resolved "https://registry.yarnpkg.com/@types/sinonjs__fake-timers/-/sinonjs__fake-timers-6.0.1.tgz#681df970358c82836b42f989188d133e218c458e"
1010+
integrity sha512-yYezQwGWty8ziyYLdZjwxyMb0CZR49h8JALHGrxjQHWlqGgc8kLdHEgWrgL0uZ29DMvEVBDnHU2Wg36zKSIUtA==
1011+
9991012
"@types/sizzle@*":
10001013
version "2.3.2"
10011014
resolved "https://registry.yarnpkg.com/@types/sizzle/-/sizzle-2.3.2.tgz#a811b8c18e2babab7d542b3365887ae2e4d9de47"
@@ -4140,6 +4153,7 @@ form-data@~2.3.2:
41404153
41414154
version "1.1.1"
41424155
resolved "https://registry.yarnpkg.com/formatio/-/formatio-1.1.1.tgz#5ed3ccd636551097383465d996199100e86161e9"
4156+
integrity sha1-XtPM1jZVEJc4NGXZlhmRAOhhYek=
41434157
dependencies:
41444158
samsam "~1.1"
41454159

@@ -6260,6 +6274,7 @@ loglevel@^1.6.0:
62606274
62616275
version "1.3.2"
62626276
resolved "https://registry.yarnpkg.com/lolex/-/lolex-1.3.2.tgz#7c3da62ffcb30f0f5a80a2566ca24e45d8a01f31"
6277+
integrity sha1-fD2mL/yzDw9agKJWbKJORdigHzE=
62636278

62646279
longest@^1.0.1:
62656280
version "1.0.1"
@@ -8249,10 +8264,12 @@ safe-regex@^1.1.0:
82498264
82508265
version "1.1.2"
82518266
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.2.tgz#bec11fdc83a9fda063401210e40176c3024d1567"
8267+
integrity sha1-vsEf3IOp/aBjQBIQ5AF2wwJNFWc=
82528268

82538269
samsam@~1.1:
82548270
version "1.1.3"
82558271
resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.3.tgz#9f5087419b4d091f232571e7fa52e90b0f552621"
8272+
integrity sha1-n1CHQZtNCR8jJXHn+lLpCw9VJiE=
82568273

82578274
sauce-connect-launcher@^1.2.2:
82588275
version "1.3.2"
@@ -8463,9 +8480,10 @@ simple-concat@^1.0.0:
84638480
version "1.0.0"
84648481
resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.0.tgz#7344cbb8b6e26fb27d66b2fc86f9f6d5997521c6"
84658482

8466-
sinon@^1.7.3:
8483+
sinon@^1.17.7:
84678484
version "1.17.7"
84688485
resolved "https://registry.yarnpkg.com/sinon/-/sinon-1.17.7.tgz#4542a4f49ba0c45c05eb2e9dd9d203e2b8efe0bf"
8486+
integrity sha1-RUKk9JugxFwF6y6d2dID4rjv4L8=
84698487
dependencies:
84708488
formatio "1.1.1"
84718489
lolex "1.3.2"
@@ -9923,13 +9941,7 @@ [email protected]:
99239941
dependencies:
99249942
inherits "2.0.1"
99259943

9926-
"util@>=0.10.3 <1":
9927-
version "0.11.0"
9928-
resolved "https://registry.yarnpkg.com/util/-/util-0.11.0.tgz#c5f391beb244103d799b21077a926fef8769e1fb"
9929-
dependencies:
9930-
inherits "2.0.3"
9931-
9932-
util@^0.12.1:
9944+
"util@>=0.10.3 <1", util@^0.12.1:
99339945
version "0.12.3"
99349946
resolved "https://registry.yarnpkg.com/util/-/util-0.12.3.tgz#971bb0292d2cc0c892dab7c6a5d37c2bec707888"
99359947
integrity sha512-I8XkoQwE+fPQEhy9v012V+TSdH2kp9ts29i20TaaDUXsg7x/onePbhFJUExBfv/2ay1ZOp/Vsm3nDlmnFGSAog==

0 commit comments

Comments
 (0)