Skip to content

Commit

Permalink
🐛Lazy generate linker values. (ampproject#18640)
Browse files Browse the repository at this point in the history
* lazy generate

* add test

* return
  • Loading branch information
calebcordry authored and Enriqe committed Nov 28, 2018
1 parent 011856e commit b235df0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
18 changes: 11 additions & 7 deletions extensions/amp-analytics/0.1/linker-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand Down Expand Up @@ -101,8 +101,7 @@ export class LinkerManager {
expandedIds[keys[i]] = value;
}
});
this.resolvedLinkers_[name] =
createLinker(/* version */ '1', expandedIds);
this.resolvedIds_[name] = expandedIds;
});
});

Expand Down Expand Up @@ -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]);
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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');
Expand Down
31 changes: 31 additions & 0 deletions extensions/amp-analytics/0.1/test/test-linker-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit b235df0

Please sign in to comment.