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

[amp-analytics] Added ability to use variables in selectors. #3281

Merged
merged 1 commit into from Jun 28, 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
14 changes: 12 additions & 2 deletions extensions/amp-analytics/0.1/amp-analytics.js
Expand Up @@ -166,8 +166,18 @@ export class AmpAnalytics extends AMP.BaseElement {
if (!result) {
return;
}
addListener(this.getWin(), trigger,
this.handleEvent_.bind(this, trigger));

if (trigger['selector']) {
// Expand the selector using variable expansion.
trigger['selector'] = this.expandTemplate_(trigger['selector'],
trigger);
addListener(this.getWin(), trigger, this.handleEvent_.bind(this,
trigger));

} else {
addListener(this.getWin(), trigger,
this.handleEvent_.bind(this, trigger));
}
}));
}
}
Expand Down
28 changes: 28 additions & 0 deletions extensions/amp-analytics/0.1/test/test-amp-analytics.js
Expand Up @@ -16,6 +16,7 @@

import {ANALYTICS_CONFIG} from '../vendors';
import {AmpAnalytics} from '../amp-analytics';
import {instrumentationServiceFor} from '../instrumentation';
import {
installUserNotificationManager,
} from '../../../amp-user-notification/0.1/amp-user-notification';
Expand Down Expand Up @@ -529,6 +530,33 @@ describe('amp-analytics', function() {
});
});

it('expands selector with config variable', () => {
const ins = instrumentationServiceFor(windowApi);
const addListenerSpy = sandbox.spy(ins, 'addListener');
const analytics = getAnalyticsTag({
requests: {foo: 'https://example.com/bar'},
triggers: [{on: 'click', selector: '${foo}', request: 'foo'}],
vars: {foo: 'bar'},
});
return waitForNoSendRequest(analytics).then(() => {
expect(addListenerSpy.callCount).to.equal(1);
expect(addListenerSpy.args[0][0]['selector']).to.equal('bar');
});
});

it('does not expands selector with platform variable', () => {
const ins = instrumentationServiceFor(windowApi);
const addListenerSpy = sandbox.spy(ins, 'addListener');
const analytics = getAnalyticsTag({
requests: {foo: 'https://example.com/bar'},
triggers: [{on: 'click', selector: '${title}', request: 'foo'}],
});
return waitForNoSendRequest(analytics).then(() => {
expect(addListenerSpy.callCount).to.equal(1);
expect(addListenerSpy.args[0][0]['selector']).to.equal('TITLE');
});
});

it('respects optout', function() {
const config = {
'requests': {'foo': 'https://example.com/bar'},
Expand Down