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

Test the URL generated for the predefined requests of all vendor configs. #2211

Merged
merged 1 commit into from
Feb 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class AmpAnalytics extends AMP.BaseElement {
if (!request) {
console./*OK*/error(this.getName_(), 'Ignoring event. Request string ' +
'not found: ', trigger['request']);
return;
return Promise.resolve();
}

// Add any given extraUrlParams as query string param
Expand All @@ -342,21 +342,23 @@ export class AmpAnalytics extends AMP.BaseElement {
const argList = match[2] || '';
const raw = (trigger['vars'] && trigger['vars'][name] ||
this.config_['vars'] && this.config_['vars'][name]);
const val = this.encodeVars_(raw != null ? raw : '');
const val = this.encodeVars_(raw != null ? raw : '', name);
return val + argList;
});

// For consistentcy with amp-pixel we also expand any url replacements.
urlReplacementsFor(this.getWin()).expand(request).then(request => {
// For consistency with amp-pixel we also expand any url replacements.
return urlReplacementsFor(this.getWin()).expand(request).then(request => {
this.sendRequest_(request, trigger);
return request;
});
}

/**
* @param {string} raw The values to URI encode.
* @param {string} unusedName Name of the variable.
* @private
*/
encodeVars_(raw) {
encodeVars_(raw, unusedName) {
if (isArray(raw)) {
return raw.map(encodeURIComponent).join(',');
}
Expand Down
60 changes: 60 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 @@ -14,6 +14,7 @@
* limitations under the License.
*/

import {ANALYTICS_CONFIG} from '../vendors';
import {AmpAnalytics} from '../../../../build/all/v0/amp-analytics-0.1.max';
import {
installUserNotificationManager
Expand All @@ -25,8 +26,11 @@ import {markElementScheduledForTesting} from '../../../../src/custom-element';
import {installCidService} from '../../../../src/service/cid-impl';
import {installViewerService} from '../../../../src/service/viewer-impl';
import {installViewportService} from '../../../../src/service/viewport-impl';
import {urlReplacementsFor} from '../../../../src/url-replacements';
import * as sinon from 'sinon';

const VENDOR_REQUESTS = require('./vendor-requests.json');

adopt(window);

describe('amp-analytics', function() {
Expand Down Expand Up @@ -64,6 +68,10 @@ describe('amp-analytics', function() {
return Promise.resolve(JSON.parse(jsonMockResponses[url]));
}};
});
const link = document.createElement('link');
link.setAttribute('rel', 'canonical');
link.setAttribute('href', './test-canonical.html');
iframe.win.document.head.appendChild(link);
windowApi = iframe.win;
});
});
Expand Down Expand Up @@ -130,6 +138,58 @@ describe('amp-analytics', function() {
});
});

describe('vendor request tests', () => {
const actualResults = {};
for (const vendor in ANALYTICS_CONFIG) {
const config = ANALYTICS_CONFIG[vendor];
if (!config.requests) {
continue;
}
actualResults[vendor] = {};
describe('analytics vendor: ' + vendor, function() {
for (const name in config.requests) {
it('should produce request: ' + name +
'. If this test fails update vendor-requests.json', () => {
const analytics = getAnalyticsTag({
requests: config.requests
});
analytics.createdCallback();
analytics.buildCallback();
const urlReplacements = urlReplacementsFor(analytics.getWin());
sandbox.stub(urlReplacements, 'getReplacement_', function(name) {
expect(this.replacements_).to.have.property(name);
return '_' + name.toLowerCase() + '_';
});
const encodeVars = analytics.encodeVars_;
sandbox.stub(analytics, 'encodeVars_', function(val, name) {
val = encodeVars.call(this, val, name);
if (val == '') {
return '$' + name;
}
return val;
});
return analytics.layoutCallback().then(() => {
return analytics.handleEvent_({
request: name
}).then(url => {
const val = VENDOR_REQUESTS[vendor][name];
if (val == null) {
throw new Error('Define ' + vendor + '.' + name +
'in vendor-requests.json. Expected value: ' + url);
}
actualResults[vendor][name] = url;
// Write this out for easy copy pasting.
// top.document.documentElement.setAttribute('json',
// JSON.stringify(actualResults, null, ' '));
expect(url).to.equal(val);
});
});
});
}
});
}
});

it('does not send a hit when config is not in a script tag', function() {
const config = JSON.stringify({
'requests': {'foo': 'https://example.com/bar'},
Expand Down
60 changes: 60 additions & 0 deletions extensions/amp-analytics/0.1/test/vendor-requests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"atinternet": {
"base": "https://$log$domain/hit.xiti?s=$site&ts=_timestamp_&r=_screen_width_x_screen_height_x_screen_color_depth_&re=_available_screen_width_x_available_screen_height_",
"suffix": "&ref=_document_referrer_",
"pageview": "https://$log$domain/hit.xiti?s=$site&ts=_timestamp_&r=_screen_width_x_screen_height_x_screen_color_depth_&re=_available_screen_width_x_available_screen_height_&p=_title_&s2=$level2&ref=_document_referrer_",
"click": "https://$log$domain/hit.xiti?s=$site&ts=_timestamp_&r=_screen_width_x_screen_height_x_screen_color_depth_&re=_available_screen_width_x_available_screen_height_&pclick=_title_&s2click=$level2&p=$label&s2=$level2Click&type=click&click=$type&ref=_document_referrer_"
},
"chartbeat": {
"host": "https://ping.chartbeat.net",
"basePrefix": "/ping?h=$domain&p=_canonical_path_&u=_client_id_&d=_canonical_host_&g=$uid&g0=$sections&g1=$authors&g2=$zone&g3=$sponsorName&g4=$contentType&x=_scroll_top_&y=_scroll_height_&j=$decayTime&r=_document_referrer_&b=_page_load_time_&t=_client_id__page_view_id_&i=_title_&T=_timestamp_&E=_total_engaged_time_&C=2&R=1&W=0&I=0&c=120",
"baseSuffix": "&_",
"interval": "https://ping.chartbeat.net/ping?h=$domain&p=_canonical_path_&u=_client_id_&d=_canonical_host_&g=$uid&g0=$sections&g1=$authors&g2=$zone&g3=$sponsorName&g4=$contentType&x=_scroll_top_&y=_scroll_height_&j=$decayTime&r=_document_referrer_&b=_page_load_time_&t=_client_id__page_view_id_&i=_title_&T=_timestamp_&E=_total_engaged_time_&C=2&R=1&W=0&I=0&c=120&_",
"anchorClick": "https://ping.chartbeat.net/ping?h=$domain&p=_canonical_path_&u=_client_id_&d=_canonical_host_&g=$uid&g0=$sections&g1=$authors&g2=$zone&g3=$sponsorName&g4=$contentType&x=_scroll_top_&y=_scroll_height_&j=$decayTime&r=_document_referrer_&b=_page_load_time_&t=_client_id__page_view_id_&i=_title_&T=_timestamp_&E=_total_engaged_time_&C=2&R=1&W=0&I=0&c=120&_"
},
"comscore": {
"host": "https://sb.scorecardresearch.com",
"base": "https://sb.scorecardresearch.com/b?",
"pageview": "https://sb.scorecardresearch.com/b?c1=2&c2=$c2&rn=_random_&c8=_title_&c7=_canonical_url_&c9=_document_referrer_&cs_c7amp=_ampdoc_url_"
},
"googleanalytics": {
"host": "https://www.google-analytics.com",
"basePrefix": "v=1&_v=a0&aip=true&_s=1&dt=_title_&sr=_screen_width_x_screen_height_&_utmht=_timestamp_&jid=&cid=_client_id_&tid=$account&dl=$documentLocation&dr=_document_referrer_&sd=_screen_color_depth_&ul=_browser_language_&de=_document_charset_",
"baseSuffix": "&a=_page_view_id_&z=_random_",
"pageview": "https://www.google-analytics.com/r/collect?v=1&_v=a0&aip=true&_s=1&dt=_title_&sr=_screen_width_x_screen_height_&_utmht=_timestamp_&jid=&cid=_client_id_&tid=$account&dl=$documentLocation&dr=_document_referrer_&sd=_screen_color_depth_&ul=_browser_language_&de=_document_charset_&t=pageview&_r=1&a=_page_view_id_&z=_random_",
"event": "https://www.google-analytics.com/collect?v=1&_v=a0&aip=true&_s=1&dt=_title_&sr=_screen_width_x_screen_height_&_utmht=_timestamp_&jid=&cid=_client_id_&tid=$account&dl=$documentLocation&dr=_document_referrer_&sd=_screen_color_depth_&ul=_browser_language_&de=_document_charset_&t=event&ec=$eventCategory&ea=$eventAction&el=$eventLabel&ev=$eventValue&a=_page_view_id_&z=_random_",
"social": "https://www.google-analytics.com/collect?v=1&_v=a0&aip=true&_s=1&dt=_title_&sr=_screen_width_x_screen_height_&_utmht=_timestamp_&jid=&cid=_client_id_&tid=$account&dl=$documentLocation&dr=_document_referrer_&sd=_screen_color_depth_&ul=_browser_language_&de=_document_charset_&t=social&sa=$socialAction&sn=$socialNetwork&st=$socialTarget&a=_page_view_id_&z=_random_",
"timing": "https://www.google-analytics.com/collect?v=1&_v=a0&aip=true&_s=1&dt=_title_&sr=_screen_width_x_screen_height_&_utmht=_timestamp_&jid=&cid=_client_id_&tid=$account&dl=$documentLocation&dr=_document_referrer_&sd=_screen_color_depth_&ul=_browser_language_&de=_document_charset_&t=timing&plt=_page_load_time_&dns=_domain_lookup_time_&tcp=_tcp_connect_time_&rrt=_redirect_time_&srt=_server_response_time_&pdt=_page_download_time_&clt=_content_load_time_&dit=_dom_interactive_time_&a=_page_view_id_&z=_random_"
},
"krux": {
"beaconHost": "https://beacon.krxd.net",
"timing": "t_navigation_type=0&t_dns=_domain_lookup_time_&t_tcp=_tcp_connect_time_&t_http_request=_server_response_time_&t_http_response=_page_download_time_&t_content_ready=_content_load_time_&t_window_load=_page_load_time_&t_redirect=_redirect_time_",
"common": "source=amp&confid=$confid&_kpid=$pubid&_kcp_s=$site&_kcp_sc=$section&_kcp_ssc=$subsection&_kcp_d=_canonical_host_&_kpref_=_document_referrer_&_kua_kx_amp_client_id=_client_id_&_kua_kx_lang=_browser_language_&_kua_kx_tech_browser_language=_browser_language_&_kua_kx_tz=_timezone_",
"pageview": "https://beacon.krxd.net/pixel.gif?source=amp&confid=$confid&_kpid=$pubid&_kcp_s=$site&_kcp_sc=$section&_kcp_ssc=$subsection&_kcp_d=_canonical_host_&_kpref_=_document_referrer_&_kua_kx_amp_client_id=_client_id_&_kua_kx_lang=_browser_language_&_kua_kx_tech_browser_language=_browser_language_&_kua_kx_tz=_timezone_&t_navigation_type=0&t_dns=_domain_lookup_time_&t_tcp=_tcp_connect_time_&t_http_request=_server_response_time_&t_http_response=_page_download_time_&t_content_ready=_content_load_time_&t_window_load=_page_load_time_&t_redirect=_redirect_time_",
"event": "https://beacon.krxd.net/event.gif?source=amp&confid=$confid&_kpid=$pubid&_kcp_s=$site&_kcp_sc=$section&_kcp_ssc=$subsection&_kcp_d=_canonical_host_&_kpref_=_document_referrer_&_kua_kx_amp_client_id=_client_id_&_kua_kx_lang=_browser_language_&_kua_kx_tech_browser_language=_browser_language_&_kua_kx_tz=_timezone_&t_navigation_type=0&t_dns=_domain_lookup_time_&t_tcp=_tcp_connect_time_&t_http_request=_server_response_time_&t_http_response=_page_download_time_&t_content_ready=_content_load_time_&t_window_load=_page_load_time_&t_redirect=_redirect_time_&pageview=false"
},
"parsely": {
"host": "https://srv.pixel.parsely.com",
"basePrefix": "https://srv.pixel.parsely.com/plogger/?rand=_timestamp_&idsite=$apikey&url=_ampdoc_url_&urlref=_document_referrer_&screen=_screen_width_x_screen_height_%7C_available_screen_width_x_available_screen_height_%7C_screen_color_depth_&title=_title_&date=_timestamp_&ampid=_client_id_",
"pageview": "https://srv.pixel.parsely.com/plogger/?rand=_timestamp_&idsite=$apikey&url=_ampdoc_url_&urlref=_document_referrer_&screen=_screen_width_x_screen_height_%7C_available_screen_width_x_available_screen_height_%7C_screen_color_depth_&title=_title_&date=_timestamp_&ampid=_client_id_&action=pageview"
},
"quantcast": {
"host": "https://pixel.quantserve.com/pixel",
"pageview": "https://pixel.quantserve.com/pixel;r=_random_;a=$pcode;labels=$labels;fpan=;fpa=_client_id_;ns=0;ce=1;cm=;je=0;sr=_screen_width_x_screen_height_x_screen_color_depth_;enc=n;et=_timestamp_;ref=_document_referrer_;url=_canonical_url_"
},
"adobeanalytics": {
"requestPath": "/b/ss/$reportSuites/0/amp-1.0/s_random_",
"basePrefix": "vid=z_client_id_&ndh=0&ce=_document_charset_&pageName=$pageName&g=_ampdoc_url_&r=_document_referrer_&bh=_available_screen_height_&bw=_available_screen_width_&c=_screen_color_depth_&j=amp&s=_screen_width_x_screen_height_",
"pageview": "https://$host/b/ss/$reportSuites/0/amp-1.0/s_random_?vid=z_client_id_&ndh=0&ce=_document_charset_&pageName=$pageName&g=_ampdoc_url_&r=_document_referrer_&bh=_available_screen_height_&bw=_available_screen_width_&c=_screen_color_depth_&j=amp&s=_screen_width_x_screen_height_",
"click": "https://$host/b/ss/$reportSuites/0/amp-1.0/s_random_?vid=z_client_id_&ndh=0&ce=_document_charset_&pageName=$pageName&g=_ampdoc_url_&r=_document_referrer_&bh=_available_screen_height_&bw=_available_screen_width_&c=_screen_color_depth_&j=amp&s=_screen_width_x_screen_height_&pe=lnk_$linkType&pev1=$linkUrl&pev2=$linkName"
},
"infonline": {
"pageview": "$url?st=$st&sv=$sv&ap=$ap&co=$co&cp=$cp&host=_canonical_host_&path=_canonical_path_"
},
"simplereach": {
"host": "https://edge.simplereach.com",
"baseParams": "amp=true&pid=$pid&title=_title_&url=_canonical_url_&date=$published_at&authors=$authors&channels=$categories&tags=$tags&referrer=_document_referrer_&page_url=_source_url_&user_id=_client_id_&domain=_canonical_host_",
"visible": "https://edge.simplereach.com/n?amp=true&pid=$pid&title=_title_&url=_canonical_url_&date=$published_at&authors=$authors&channels=$categories&tags=$tags&referrer=_document_referrer_&page_url=_source_url_&user_id=_client_id_&domain=_canonical_host_",
"timer": "https://edge.simplereach.com/t?amp=true&pid=$pid&title=_title_&url=_canonical_url_&date=$published_at&authors=$authors&channels=$categories&tags=$tags&referrer=_document_referrer_&page_url=_source_url_&user_id=_client_id_&domain=_canonical_host_&t=5000&e=5000"
}
}
10 changes: 9 additions & 1 deletion src/url-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class UrlReplacements {
args = opt_strargs.split(',');
}
const binding = (opt_bindings && (name in opt_bindings)) ?
opt_bindings[name] : this.replacements_[name];
opt_bindings[name] : this.getReplacement_(name);
const val = (typeof binding == 'function') ?
binding.apply(null, args) : binding;
// In case the produced value is a promise, we don't actually
Expand Down Expand Up @@ -405,6 +405,14 @@ class UrlReplacements {
return replacementPromise || Promise.resolve(url);
}

/**
* @param {string} name
* @return {function(*):*}
*/
getReplacement_(name) {
return this.replacements_[name];
}

/**
* @param {!Object<string, *>=} opt_bindings
* @return {!RegExp}
Expand Down