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

Allow URL var substitution in the analytics config request. #2093

Merged
merged 1 commit into from
Feb 18, 2016
Merged
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
10 changes: 7 additions & 3 deletions extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class AmpAnalytics extends AMP.BaseElement {
* @return {!Promise<>}
*/
fetchRemoteConfig_() {
const remoteConfigUrl = this.element.getAttribute('config');
let remoteConfigUrl = this.element.getAttribute('config');
if (!remoteConfigUrl) {
return Promise.resolve();
}
Expand All @@ -188,13 +188,17 @@ export class AmpAnalytics extends AMP.BaseElement {
if (this.element.hasAttribute('data-credentials')) {
fetchConfig.credentials = this.element.getAttribute('data-credentials');
}
return xhrFor(this.getWin()).fetchJson(remoteConfigUrl, fetchConfig)
return urlReplacementsFor(this.getWin()).expand(remoteConfigUrl)
.then(expandedUrl => {
remoteConfigUrl = expandedUrl;
return xhrFor(this.getWin()).fetchJson(remoteConfigUrl, fetchConfig);
})
.then(jsonValue => {
this.remoteConfig_ = jsonValue;
log.fine(this.getName_(), 'Remote config loaded', remoteConfigUrl);
}, err => {
console./*OK*/error(this.getName_(), 'Error loading remote config: ',
remoteConfigUrl, err);
remoteConfigUrl, err);
});
}

Expand Down
15 changes: 14 additions & 1 deletion extensions/amp-analytics/0.1/test/test-amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ describe('amp-analytics', function() {
let uidService;

const jsonMockResponses = {
'config1': '{"vars": {"title": "remote"}}'
'config1': '{"vars": {"title": "remote"}}',
'https://foo/Test%20Title': '{"vars": {"title": "magic"}}'
};

beforeEach(() => {
Expand Down Expand Up @@ -588,6 +589,18 @@ describe('amp-analytics', function() {
});
});

it('expands urls in config request', () => {
const analytics = getAnalyticsTag({
'requests': {'foo': 'https://example.com/${title}'},
'triggers': [{'on': 'visible', 'request': 'foo'}]
}, {
'config': 'https://foo/TITLE'
});
return waitForSendRequest(analytics).then(() => {
expect(sendRequestSpy.args[0][0]).to.equal('https://example.com/magic');
});
});

it('updates requestCount on each request', () => {
const analytics = getAnalyticsTag({
'host': 'example.com',
Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-analytics/amp-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Adds support for Quantcast Measurement. More details for adding Quantcast Measur
#### <a name="attributes"></a>Attributes

- `type` See [Analytics vendors](#analytics-vendors)
- `config` Optional attribute. This attribute can be used to load a configuration from a specified remote URL. The URL specified here should use https scheme. See also `data-include-credentials` attribute below.
- `config` Optional attribute. This attribute can be used to load a configuration from a specified remote URL. The URL specified here should use https scheme. See also `data-include-credentials` attribute below. The URL may include [AMP URL vars](../../spec/amp-var-substitutions.md).

```
<amp-analytics config="https://example.com/analytics.config.json"></amp-analytics>
Expand Down