Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add requestOrigin field in analytics config #22454

Merged
merged 20 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ export class AmpAnalytics extends AMP.BaseElement {
for (const k in this.config_['requests']) {
if (hasOwn(this.config_['requests'], k)) {
const request = this.config_['requests'][k];

requests[k] = new RequestHandler(
this.element,
request,
Expand Down
23 changes: 23 additions & 0 deletions extensions/amp-analytics/0.1/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,32 @@ export class AnalyticsConfig {

return this.fetchRemoteConfig_()
.then(this.processConfigs_.bind(this))
.then(this.handleTopLevelAttributes_.bind(this))
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
.then(() => this.config_);
}

/**
* Handles top level fields in config
*/
handleTopLevelAttributes_() {
// handle a top level requestOrigin
if (
hasOwn(this.config_, 'requests') &&
hasOwn(this.config_, 'requestOrigin')
) {
const requestOrigin = this.config_['requestOrigin'];

for (const requestName in this.config_['requests']) {
// only add top level request origin into request if it doesn't have one
if (!hasOwn(this.config_['requests'][requestName], 'requestOrigin')) {
this.config_['requests'][requestName][
'requestOrigin'
] = requestOrigin;
}
}
}
}

/**
* Returns a promise that resolves when remote config is ready (or
* immediately if no remote config is specified.)
Expand Down
59 changes: 56 additions & 3 deletions extensions/amp-analytics/0.1/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import {
} from './variables';
import {SANDBOX_AVAILABLE_VARS} from './sandbox-vars-whitelist';
import {Services} from '../../../src/services';
import {devAssert, userAssert} from '../../../src/log';
import {devAssert, user, userAssert} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {getResourceTiming} from './resource-timing';
import {isArray, isFiniteNumber, isObject} from '../../../src/types';

const TAG = 'amp-analytics/requests';
const BATCH_INTERVAL_MIN = 200;

export class RequestHandler {
Expand All @@ -47,6 +48,9 @@ export class RequestHandler {
/** @const {!Window} */
this.win = this.ampdoc_.win;

/** @const {string} !if specified, all requests are prepended with this */
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
this.requestOrigin_ = request['requestOrigin'];

/** @const {string} */
this.baseUrl = devAssert(request['baseUrl']);

Expand All @@ -71,6 +75,9 @@ export class RequestHandler {
/** @private {?Promise<string>} */
this.baseUrlTemplatePromise_ = null;

/** @private {?Promise<string>} */
this.requestOriginPromise_ = null;

/** @private {!Array<!Promise<!BatchSegmentDef>>} */
this.batchSegmentPromises_ = [];

Expand Down Expand Up @@ -133,10 +140,35 @@ export class RequestHandler {

if (!this.baseUrlPromise_) {
expansionOption.freezeVar('extraUrlParams');
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved

// expand requestOrigin if it is declared
if (this.requestOrigin_) {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
// do not encode vars in request origin
const requestOriginExpansionOpt = new ExpansionOptions(
expansionOption.vars,
expansionOption.iterations,
true // opt_noEncode
);

this.requestOriginPromise_ = this.variableService_
// expand variables in request origin
.expandTemplate(this.requestOrigin_, requestOriginExpansionOpt)
// substitute in URL values e.g. DOCUMENT_REFERRER -> https://example.com
.then(expandedRequestOrigin => {
return this.urlReplacementService_.expandUrlAsync(
expandedRequestOrigin,
bindings,
this.whiteList_,
true // opt_noEncode
);
});
}

this.baseUrlTemplatePromise_ = this.variableService_.expandTemplate(
this.baseUrl,
expansionOption
);

this.baseUrlPromise_ = this.baseUrlTemplatePromise_.then(baseUrl => {
return this.urlReplacementService_.expandUrlAsync(
baseUrl,
Expand Down Expand Up @@ -207,18 +239,39 @@ export class RequestHandler {
*/
fire_() {
const {
requestOriginPromise_: requestOriginPromise,
baseUrlTemplatePromise_: baseUrlTemplatePromise,
baseUrlPromise_: baseUrlPromise,
batchSegmentPromises_: segmentPromises,
} = this;
const trigger = /** @type {!JsonObject} */ (this.lastTrigger_);
this.reset_();

baseUrlTemplatePromise.then(preUrl => {
// preconnect to requestOrigin if available, otherwise baseUrlTemplate
const preconnectPromise = requestOriginPromise
? requestOriginPromise
: baseUrlTemplatePromise;

preconnectPromise.then(preUrl => {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
this.preconnect_.url(preUrl, true);
Promise.all([baseUrlPromise, Promise.all(segmentPromises)]).then(
results => {
const baseUrl = results[0];
// prepend requestOrigin if available
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
let baseUrl;
if (requestOriginPromise) {
try {
baseUrl = new URL(results[0], preUrl).href;
} catch (e) {
user().error(
TAG,
'Unable to construct URL with ' + preUrl + ' and ' + results[0]
);
return;
}
} else {
baseUrl = results[0];
}

const batchSegments = results[1];
if (batchSegments.length === 0) {
return;
Expand Down
42 changes: 42 additions & 0 deletions extensions/amp-analytics/0.1/test/test-requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ describes.realWin('Requests', {amp: 1}, env => {
}

describe('RequestHandler', () => {
describe('send with request origin', () => {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
let spy;
beforeEach(() => {
spy = sandbox.spy();
});

it('should prepend request origin', function*() {
const r = {'baseUrl': '/r1', 'requestOrigin': 'http://example.com'};
const handler = createRequestHandler(r, spy);
const expansionOptions = new ExpansionOptions({});

handler.send({}, {}, expansionOptions, {});
yield macroTask();
expect(spy).to.be.calledWith('http://example.com/r1');
});

it('should expand request origin', function*() {
const r = {'baseUrl': '/r2', 'requestOrigin': '${documentReferrer}'};
const handler = createRequestHandler(r, spy);
const expansionOptions = new ExpansionOptions({
'documentReferrer': 'http://example.com',
});

handler.send({}, {}, expansionOptions, {});
yield macroTask();
expect(spy).to.be.calledWith('http://example.com/r2');
});

it('should expand nested request origin', function*() {
const r = {'baseUrl': '/r3', 'requestOrigin': '${a}'};
const handler = createRequestHandler(r, spy);
const expansionOptions = new ExpansionOptions({
'a': '${b}',
'b': 'http://example.com',
});

handler.send({}, {}, expansionOptions, {});
yield macroTask();
expect(spy).to.be.calledWith('http://example.com/r3');
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We talked about this, but I still think it's worth adding the requestOrigin being relative url test. Tests are mostly for developers, and it helps them to understand the desired behavior much better. With that test, we could save a lot of explanation on how we want to handle the special case where requestOrigin is not absolute. WDYT?


describe('batch', () => {
it('should batch multiple send', function*() {
const spy = sandbox.spy();
Expand Down
15 changes: 15 additions & 0 deletions extensions/amp-analytics/0.1/variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ export class ExpansionOptions {
}
return value;
}

/**
* Creates a copy of this instance of ExpansionOptions
* @return {ExpansionOptions}
*/
makeCopy() {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
const copy = new ExpansionOptions(
this.vars,
this.iterations,
this.noEncode
);
copy.freezeVars = this.freezeVars;
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved

return copy;
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/service/url-replacements-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1019,15 +1019,17 @@ export class UrlReplacements {
* @param {!Object<string, *>=} opt_bindings
* @param {!Object<string, boolean>=} opt_whiteList Optional white list of names
* that can be substituted.
* @param {boolean=} opt_noEncode should not encode URL
* @return {!Promise<string>}
*/
expandUrlAsync(url, opt_bindings, opt_whiteList) {
expandUrlAsync(url, opt_bindings, opt_whiteList, opt_noEncode) {
return /** @type {!Promise<string>} */ (new Expander(
this.variableSource_,
opt_bindings,
/* opt_collectVars */ undefined,
/* opt_sync */ undefined,
opt_whiteList
opt_whiteList,
opt_noEncode
)
./*OK*/ expand(url)
.then(replacement => this.ensureProtocolMatches_(url, replacement)));
Expand Down