From b235df0177e1f4d693e1b54cba98930735076d8b Mon Sep 17 00:00:00 2001 From: Caleb Cordry Date: Tue, 9 Oct 2018 18:00:20 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9BLazy=20generate=20linker=20values.?= =?UTF-8?q?=20(#18640)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lazy generate * add test * return --- .../amp-analytics/0.1/linker-manager.js | 18 ++++++----- .../0.1/test/test-linker-manager.js | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/extensions/amp-analytics/0.1/linker-manager.js b/extensions/amp-analytics/0.1/linker-manager.js index 5afc8b03f3537..57ff974cb09d9 100644 --- a/extensions/amp-analytics/0.1/linker-manager.js +++ b/extensions/amp-analytics/0.1/linker-manager.js @@ -53,7 +53,7 @@ export class LinkerManager { this.allLinkerPromises_ = []; /** @private {!JsonObject} */ - this.resolvedLinkers_ = dict(); + this.resolvedIds_ = dict(); /** @private {!../../../src/service/url-impl.Url} */ this.urlService_ = Services.urlForDoc(this.ampdoc_); @@ -101,8 +101,7 @@ export class LinkerManager { expandedIds[keys[i]] = value; } }); - this.resolvedLinkers_[name] = - createLinker(/* version */ '1', expandedIds); + this.resolvedIds_[name] = expandedIds; }); }); @@ -232,7 +231,7 @@ export class LinkerManager { for (const linkerName in linkerConfigs) { // The linker param is created asynchronously. This callback should be // synchronous, so we skip if value is not there yet. - if (this.resolvedLinkers_[linkerName]) { + if (this.resolvedIds_[linkerName]) { this.maybeAppendLinker_(element, linkerName, linkerConfigs[linkerName]); } } @@ -253,7 +252,9 @@ export class LinkerManager { const /** @type {Array} */ domains = config['destinationDomains']; if (this.isDomainMatch_(hostname, domains)) { - el.href = addParamToUrl(href, name, this.resolvedLinkers_[name]); + const linkerValue = createLinker(/* version */ '1', + this.resolvedIds_[name]); + el.href = addParamToUrl(href, name, linkerValue); } } @@ -345,11 +346,14 @@ export class LinkerManager { * @param {string} linkerName */ addDataToForm_(form, actionXhrMutator, linkerName) { - const linkerValue = this.resolvedLinkers_[linkerName]; - if (!linkerValue) { + const ids = this.resolvedIds_[linkerName]; + if (!ids) { + // Form was clicked before macros resolved. return; } + const linkerValue = createLinker(/* version */ '1', ids); + // Runtime controls submits with `action-xhr`, so we can append the linker // param const actionXhrUrl = form.getAttribute('action-xhr'); diff --git a/extensions/amp-analytics/0.1/test/test-linker-manager.js b/extensions/amp-analytics/0.1/test/test-linker-manager.js index 355e642104170..783b063dcac36 100644 --- a/extensions/amp-analytics/0.1/test/test-linker-manager.js +++ b/extensions/amp-analytics/0.1/test/test-linker-manager.js @@ -18,6 +18,10 @@ import * as experiments from '../../../../src/experiments'; import {LinkerManager, areFriendlyDomains} from '../linker-manager'; import {Priority} from '../../../../src/service/navigation'; import {Services} from '../../../../src/services'; +import { + installLinkerReaderService, + linkerReaderServiceFor, +} from '../linker-reader'; import {installVariableService} from '../variables'; import {mockWindowInterface} from '../../../../testing/test-helper'; import {toggleExperiment} from '../../../../src/experiments'; @@ -179,6 +183,33 @@ describes.realWin('Linker Manager', {amp: true}, env => { }); }); + it('should generate a param valid for ingestion 5 min later', () => { + const clock = sandbox.useFakeTimers(1533329483292); + sandbox.stub(Date.prototype, 'getTimezoneOffset').returns(420); + const config = { + linkers: { + enabled: true, + testLinker: { + ids: { + cid: '12345', + }, + }, + }, + }; + + return new LinkerManager(ampdoc, config, null).init().then(() => { + clock.tick(1000 * 60 * 5); // 5 minutes. + const linkerUrl = clickAnchor('https://www.source.com/dest?a=1'); + + windowInterface.history = {replaceState: () => {}}; + windowInterface.location = {href: linkerUrl}; + + installLinkerReaderService(windowInterface); + const linkerReader = linkerReaderServiceFor(windowInterface); + return expect(linkerReader.get('testLinker', 'cid')).to.equal('12345'); + }); + }); + it('should respect destinationDomains config', () => { const config = { linkers: {