Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Remove the handling of titles from urlChangeTracker #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 5 additions & 7 deletions lib/plugins/url-change-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ class UrlChangeTracker {
}

/**
* Updates the page and title fields on the tracker and sends a pageview
* if a new history entry was created.
* Updates the page field on the tracker and sends a pageview if a new
* history entry was created.
* Note: we don't update the title because analytics.js automatically uses
* the value of `document.title`, so we rely on the SPA code to do that.
* @param {boolean} historyDidUpdate True if the history was changed via
* `pushState()` or the `popstate` event. False if the history was just
* modified via `replaceState()`.
Expand All @@ -121,10 +123,7 @@ class UrlChangeTracker {
this.path = newPath;

/** @type {FieldsObj} */
const newFields = {
page: newPath,
title: document.title,
};
const newFields = {page: newPath};

this.tracker.set(newFields);

Expand All @@ -148,7 +147,6 @@ class UrlChangeTracker {
const defaultFields = {
transport: 'beacon',
page: fieldsObj.page,
title: fieldsObj.title,
queueTime: now() - time,
};

Expand Down
67 changes: 42 additions & 25 deletions test/e2e/url-change-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,46 +60,60 @@ describe('urlTracker', function() {
browser.execute(ga.run, 'require', 'urlChangeTracker');

browser.click('#foo');
browser.waitUntil(log.hitCountEquals(1));

let hits = log.getHits();
assert.strictEqual(hits[0].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[0].dt, 'Foo');
assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/foo.html`);

browser.click('#bar');
browser.waitUntil(log.hitCountEquals(2));

hits = log.getHits();
assert.strictEqual(hits[1].dp, '/test/e2e/fixtures/bar.html');
assert.strictEqual(hits[1].dt, 'Bar');
assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/bar.html`);

browser.click('#qux');
browser.waitUntil(log.hitCountEquals(3));

hits = log.getHits();
assert.strictEqual(hits[2].dp, '/test/e2e/fixtures/qux.html');
assert.strictEqual(hits[2].dt, 'Qux');
assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/qux.html`);

browser.back();
browser.waitUntil(log.hitCountEquals(4));

hits = log.getHits();
assert.strictEqual(hits[3].dp, '/test/e2e/fixtures/bar.html');
assert.strictEqual(hits[3].dt, 'Bar');
assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/bar.html`);

browser.back();
browser.waitUntil(log.hitCountEquals(5));

hits = log.getHits();
assert.strictEqual(hits[4].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[4].dt, 'Foo');
assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/foo.html`);

browser.back();
assert.strictEqual(
browser.url().value,
`${BASE_URL}/test/e2e/fixtures/url-change-tracker.html`);

browser.waitUntil(log.hitCountEquals(6));

const hits = log.getHits();
assert.strictEqual(hits[0].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[0].dt, 'Foo');
assert.strictEqual(hits[1].dp, '/test/e2e/fixtures/bar.html');
assert.strictEqual(hits[1].dt, 'Bar');
assert.strictEqual(hits[2].dp, '/test/e2e/fixtures/qux.html');
assert.strictEqual(hits[2].dt, 'Qux');
assert.strictEqual(hits[3].dp, '/test/e2e/fixtures/bar.html');
assert.strictEqual(hits[3].dt, 'Bar');
assert.strictEqual(hits[4].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[4].dt, 'Foo');
hits = log.getHits();
assert.strictEqual(hits[5].dp,
'/test/e2e/fixtures/url-change-tracker.html');
assert.strictEqual(hits[5].dt, 'Home');
assert.strictEqual(
browser.url().value,
`${BASE_URL}/test/e2e/fixtures/url-change-tracker.html`);
});

it('updates the tracker but does not send hits when using replaceState',
Expand Down Expand Up @@ -171,24 +185,27 @@ describe('urlTracker', function() {
});

browser.click('#foo');
browser.waitUntil(log.hitCountEquals(1));

let hits = log.getHits();
assert.strictEqual(hits[0].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[0].dt, 'Foo');
assert.strictEqual(hits[0].cd1, 'urlChangeTracker');

assert.strictEqual(
browser.url().value, `${BASE_URL}/test/e2e/fixtures/foo.html`);

browser.back();
assert.strictEqual(
browser.url().value,
`${BASE_URL}/test/e2e/fixtures/url-change-tracker.html`);

browser.waitUntil(log.hitCountEquals(2));

const hits = log.getHits();
assert.strictEqual(hits[0].dp, '/test/e2e/fixtures/foo.html');
assert.strictEqual(hits[0].dt, 'Foo');
assert.strictEqual(hits[0].cd1, 'urlChangeTracker');
hits = log.getHits();
assert.strictEqual(hits[1].dp,
'/test/e2e/fixtures/url-change-tracker.html');
assert.strictEqual(hits[1].dt, 'Home');
assert.strictEqual(hits[1].cd1, 'urlChangeTracker');
assert.strictEqual(
browser.url().value,
`${BASE_URL}/test/e2e/fixtures/url-change-tracker.html`);
});

it('supports specifying a hit filter', () => {
Expand Down Expand Up @@ -249,8 +266,8 @@ function requireUrlChangeTrackerTracker_shouldTrackUrlChange() {
function requireUrlChangeTrackerTracker_hitFilter() {
ga('require', 'urlChangeTracker', {
hitFilter: (model) => {
const title = model.get('title');
if (title == 'Foo') {
const title = model.get('page');
if (title.indexOf('foo') >= 0) {
throw new Error('Exclude Foo pages');
} else {
model.set('dimension1', 'urlChangeTracker', true);
Expand Down