Skip to content

Commit

Permalink
Merge pull request #1932 from jmadler/analytics-customvars
Browse files Browse the repository at this point in the history
Add extraUrlParams and extraUrlParamsReplaceMap to amp-analytics
  • Loading branch information
cramforce committed Feb 12, 2016
2 parents e6d22e5 + 42726fc commit 7353601
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
36 changes: 35 additions & 1 deletion extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import {ANALYTICS_CONFIG} from './vendors';
import {addListener, instrumentationServiceFor} from './instrumentation';
import {assertHttpsUrl} from '../../../src/url';
import {assertHttpsUrl, addParamsToUrl} from '../../../src/url';
import {expandTemplate} from '../../../src/string';
import {installCidService} from '../../../src/service/cid-impl';
import {installStorageService} from '../../../src/service/storage-impl';
Expand All @@ -33,6 +33,7 @@ installCidService(AMP.win);
installStorageService(AMP.win);
instrumentationServiceFor(AMP.win);

const MAX_REPLACES = 16; // The maximum number of entries in a extraUrlParamsReplaceMap

export class AmpAnalytics extends AMP.BaseElement {

Expand Down Expand Up @@ -123,6 +124,34 @@ export class AmpAnalytics extends AMP.BaseElement {
'config. No analytics data will be sent.');
return Promise.resolve();
}
if (this.config_['extraUrlParams'] &&
this.config_['extraUrlParamsReplaceMap']) {
// If the config includes a extraUrlParamsReplaceMap, apply it as a set
// of params to String.replace to allow aliasing of the keys in
// extraUrlParams.
let count = 0;
for (const replaceMapKey in this.config_['extraUrlParamsReplaceMap']) {
if (++count > MAX_REPLACES) {
console./*OK*/error(this.getName_(),
"More than " + MAX_REPLACES.toString() +
" extraUrlParamsReplaceMap rules aren't allowed; Skipping the rest"
);
break;
}

for (const extraUrlParamsKey in this.config_['extraUrlParams']) {
const newkey = extraUrlParamsKey.replace(
replaceMapKey,
this.config_['extraUrlParamsReplaceMap'][replaceMapKey]
);
if (extraUrlParamsKey != newkey) {
const value = this.config_['extraUrlParams'][extraUrlParamsKey];
delete this.config_['extraUrlParams'][extraUrlParamsKey];
this.config_['extraUrlParams'][newkey] = value;
}
}
}
}

// Trigger callback can be synchronous. Do the registration at the end.
for (const k in this.config_['triggers']) {
Expand Down Expand Up @@ -285,6 +314,11 @@ export class AmpAnalytics extends AMP.BaseElement {
return;
}

// Add any given extraUrlParams as query string param
if (this.config_['extraUrlParams']) {
request = addParamsToUrl(request, this.config_['extraUrlParams']);
}

this.config_['vars']['requestCount']++;

// Replace placeholders with URI encoded values.
Expand Down
33 changes: 33 additions & 0 deletions extensions/amp-analytics/0.1/test/test-amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,39 @@ describe('amp-analytics', function() {
});
});

it('sends extraUrlParams', () => {
const analytics = getAnalyticsTag({
'extraUrlParams': {'s.evar0': '0', 's.evar1': '1', 'foofoo': 'baz'},
'requests': {'foo': 'https://example.com/${title}'},
'triggers': [{'on': 'visible', 'request': 'foo'}]
}, {
'config': 'config1'
});
return analytics.layoutCallback().then(() => {
expect(sendRequestSpy.args[0][0]).to.have.string('s.evar0=0');
expect(sendRequestSpy.args[0][0]).to.have.string('s.evar1=1');
expect(sendRequestSpy.args[0][0]).to.have.string('foofoo=baz');
});
});

it('handles extraUrlParamsReplaceMap', () => {
const analytics = getAnalyticsTag({
'extraUrlParams': {'s.evar0': '0', 's.evar1': '1', 'foofoo': 'baz'},
'extraUrlParamsReplaceMap': {'s.evar': 'v'},
'requests': {'foo': 'https://example.com/${title}'},
'triggers': [{'on': 'visible', 'request': 'foo'}]
}, {
'config': 'config1'
});
return analytics.layoutCallback().then(() => {
expect(sendRequestSpy.args[0][0]).to.have.string('v0=0');
expect(sendRequestSpy.args[0][0]).to.have.string('v1=1');
expect(sendRequestSpy.args[0][0]).to.not.have.string('s.evar1');
expect(sendRequestSpy.args[0][0]).to.not.have.string('s.evar0');
expect(sendRequestSpy.args[0][0]).to.have.string('foofoo=baz');
});
});

it('fetches and merges remote config', () => {
const analytics = getAnalyticsTag({
'vars': {'title': 'local'},
Expand Down
7 changes: 7 additions & 0 deletions extensions/amp-analytics/amp-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,10 @@ In the example below, `beacon` and `xhrpost` are set to `false`, so they will no
'image': true
}
```


### Extra URL Params

The `extraUrlParams` attribute specifies additional parameters to append to the query string of the url via the usual "&foo=baz" convention.

The `extraUrlParamsReplaceMap` attribute specifies a map of keys and values that act as parameters to String.replace() to preprocess keys in the extraUrlParams map.

0 comments on commit 7353601

Please sign in to comment.