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 16 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
24 changes: 23 additions & 1 deletion extensions/amp-analytics/0.1/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ export function expandConfigRequest(config) {
config['requests'][k] = expandRequestStr(config['requests'][k]);
}
}
return config;

return handleTopLevelAttributes_(config);
}

/**
Expand All @@ -490,3 +491,24 @@ function expandRequestStr(request) {
'baseUrl': request,
};
}

/**
* Handles top level fields in the given config
* @param {!JsonObject} config
* @return {JsonObject}
*/
function handleTopLevelAttributes_(config) {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
// handle a top level requestOrigin
if (hasOwn(config, 'requests') && hasOwn(config, 'requestOrigin')) {
const requestOrigin = config['requestOrigin'];

for (const requestName in config['requests']) {
// only add top level request origin into request if it doesn't have one
if (!hasOwn(config['requests'][requestName], 'origin')) {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
config['requests'][requestName]['origin'] = requestOrigin;
}
}
}

return config;
}
123 changes: 98 additions & 25 deletions extensions/amp-analytics/0.1/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ 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 {extractDomain} from '../../../src/url';
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 +49,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['origin'];

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

Expand All @@ -71,6 +76,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 +141,12 @@ export class RequestHandler {

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

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

this.baseUrlPromise_ = this.baseUrlTemplatePromise_.then(baseUrl => {
return this.urlReplacementService_.expandUrlAsync(
baseUrl,
Expand All @@ -146,6 +156,29 @@ export class RequestHandler {
});
}

// expand requestOrigin if it is declared
if (!this.requestOriginPromise_ && this.requestOrigin_) {
// 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
);
});
}

const params = Object.assign({}, configParams, trigger['extraUrlParams']);
const timestamp = this.win.Date.now();
const batchSegmentPromise = expandExtraUrlParams(
Expand Down Expand Up @@ -207,38 +240,48 @@ 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];
const batchSegments = results[1];
if (batchSegments.length === 0) {
return;
}
// TODO: iframePing will not work with batch. Add a config validation.
if (trigger['iframePing']) {
userAssert(
trigger['on'] == 'visible',
'iframePing is only available on page view requests.'
);
this.transport_.sendRequestUsingIframe(baseUrl, batchSegments[0]);
} else {
this.transport_.sendRequest(
baseUrl,
batchSegments,
!!this.batchInterval_
);
}
}
);
});

Promise.all([
baseUrlPromise,
Promise.all(segmentPromises),
requestOriginPromise,
]).then(results => {
const requestUrl = composeRequestUrl_(results[0], results[2]);

const batchSegments = results[1];
if (batchSegments.length === 0) {
return;
}
// TODO: iframePing will not work with batch. Add a config validation.
if (trigger['iframePing']) {
userAssert(
trigger['on'] == 'visible',
'iframePing is only available on page view requests.'
);
this.transport_.sendRequestUsingIframe(requestUrl, batchSegments[0]);
} else {
this.transport_.sendRequest(
requestUrl,
batchSegments,
!!this.batchInterval_
);
}
});
}

Expand Down Expand Up @@ -428,3 +471,33 @@ function expandExtraUrlParams(

return Promise.all(requestPromises).then(() => params);
}

/**
* Composes a request URL given a base and requestOrigin
* @private
* @param {string} baseUrl
* @param {string=} opt_requestOrigin
* @return {string}
*/
function composeRequestUrl_(baseUrl, opt_requestOrigin) {
if (opt_requestOrigin === '') {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
user().error(TAG, 'Request Origin cannot be an empty string');
return '';
}
// prepend requestOrigin if available
else if (opt_requestOrigin) {
const domain = extractDomain(opt_requestOrigin);

if (!domain) {
user().error(
TAG,
'Unable to extract domain from requestOrigin: ' + opt_requestOrigin
);
return '';
}

return domain + baseUrl;
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
}

return baseUrl;
}
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', 'origin': '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', 'origin': '${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', 'origin': '${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
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
15 changes: 15 additions & 0 deletions src/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,3 +649,18 @@ export function checkCorsUrl(url) {
export function tryDecodeUriComponent(component, opt_fallback) {
return tryDecodeUriComponent_(component, opt_fallback);
}

/**
* Extract and return the domain from the given url string via regex
* Ex. 'http://google.com?test' -> 'http://google.com'
* 'https://google.com/test1/test2' -> 'https://google.com'
* 'http://localhost:8000/test' -> 'http://localhost:8000'
* @param {string} url
* @return {string|undefined}
*/
export function extractDomain(url) {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
// regex adapted from https://stackoverflow.com/a/8498629
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
const matches = url.match(/(^https?\:\/\/[^\/?#]+)(?:[\/:?#]|$)/i);
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved

return matches ? matches[1] : undefined;
}
37 changes: 37 additions & 0 deletions test/unit/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
addParamsToUrl,
assertAbsoluteHttpOrHttpsUrl,
assertHttpsUrl,
extractDomain,
getCorsUrl,
getProxyServingType,
getSourceOrigin,
Expand Down Expand Up @@ -1108,3 +1109,39 @@ describe('getProxyServingType', () => {
).to.equal('test');
});
});

describe('extractDomain', () => {
jonathantyng-amp marked this conversation as resolved.
Show resolved Hide resolved
it('should handle query params', () => {
expect(extractDomain('http://google.com?test')).to.equal(
'http://google.com'
);
});

it('should handle anchor tags', () => {
expect(extractDomain('http://google.com#test')).to.equal(
'http://google.com'
);
});

it('should handle appended relative path', () => {
expect(extractDomain('http://google.com/test1/test2')).to.equal(
'http://google.com'
);
});

it('should handle https', () => {
expect(extractDomain('https://google.com/test')).to.equal(
'https://google.com'
);
});

it('should handle localhost', () => {
expect(extractDomain('http://localhost:8000')).to.equal(
'http://localhost:8000'
);
});

it('should handle no valid match', () => {
expect(extractDomain('/invalid')).to.equal(undefined);
});
});