Skip to content

Commit

Permalink
Merge pull request ampproject#1598 from avimehta/hitCount
Browse files Browse the repository at this point in the history
Added a new variable called requestCount.
  • Loading branch information
cramforce committed Jan 29, 2016
2 parents f1bc9eb + 818125b commit 66ec7f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
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 @@ -193,13 +193,16 @@ export class AmpAnalytics extends AMP.BaseElement {
'parsed. Is it in a valid JSON format?', er);
}

const config = {};
// Initialize config with analytics related vars.
const config = {
'vars': {
'requestCount': 0
}
};
const defaultConfig = this.predefinedConfig_['default'] || {};
const typeConfig = this.predefinedConfig_[
this.element.getAttribute('type')] || {};

config['vars'] = config['vars'] || {};

this.mergeObjects_(defaultConfig, config);
this.mergeObjects_(typeConfig, config);
this.mergeObjects_(inlineConfig, config);
Expand Down Expand Up @@ -284,6 +287,7 @@ export class AmpAnalytics extends AMP.BaseElement {
(this.config_['vars'] && this.config_['vars'][name]) || '');
return val + argList;
});
this.config_['vars']['requestCount']++;

// For consistentcy with amp-pixel we also expand any url replacements.
urlReplacementsFor(this.getWin()).expand(request).then(
Expand Down
18 changes: 18 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 @@ -481,6 +481,24 @@ describe('amp-analytics', function() {
});
});

it('updates requestCount on each request', () => {
const analytics = getAnalyticsTag({
'host': 'example.com',
'requests': {
'pageview1': '/test1=${requestCount}',
'pageview2': '/test2=${requestCount}'
},
'triggers': [
{'on': 'visible', 'request': 'pageview1'},
{'on': 'visible', 'request': 'pageview2'}
]});
analytics.layoutCallback().then(() => {
expect(sendRequestSpy.calledTwice).to.be.true;
expect(sendRequestSpy.args[0][0]).to.equal('https://example.com/test1=1');
expect(sendRequestSpy.args[1][0]).to.equal('https://example.com/test2=2');
});
});

describe('data-consent-notification-id', () => {

it('should resume fetch when consent is given', () => {
Expand Down
5 changes: 3 additions & 2 deletions extensions/amp-analytics/0.1/vendors.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export const ANALYTICS_CONFIG = {
},
'requests': {
'host': 'https://www.google-analytics.com',
'basePrefix': 'v=1&_v=a0&aip=true&_s=${hitCount}&dr=${documentReferrer}' +
'basePrefix': 'v=1&_v=a0&aip=true&_s=${requestCount}' +
'dt=${title}&sr=${screenWidth}x${screenHeight}&_utmht=${timestamp}&' +
'jid=&cid=${clientId(_ga)}&tid=${account}&dl=${documentLocation}',
'jid=&cid=${clientId(_ga)}&tid=${account}&dl=${documentLocation}&' +
'dr=${documentReferrer}',
'baseSuffix': '&a=${pageViewId}&z=${random}',
'pageview': '${host}/r/collect?${basePrefix}&t=pageview&' +
'_r=1${baseSuffix}',
Expand Down
6 changes: 6 additions & 0 deletions extensions/amp-analytics/analytics-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,9 @@ Provides the number of seconds that have elapsed since 1970. (Epoch time)
Example value: `1452710304312`
## requestCount
Provides the number of requests sent out from a particular `amp-analytics` tag. This value can be used to reconstruct the sequence in which requests were sent from a tag. The value starts from 0 and increases monotonically. Note that there may be a gap in requestCount numbers if the request sending fails due to network issues.
Example value: `6`

0 comments on commit 66ec7f8

Please sign in to comment.