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

Change amp-analytics triggers from arrays to objects. #1193

Merged
merged 1 commit into from
Dec 18, 2015
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
64 changes: 35 additions & 29 deletions examples/analytics.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
"vars": {
"title": "Example Request"
},
"triggers": [{
"on": "visible",
"request": "event",
"vars": {
"eventName": "page-loaded",
"eventId": "42"
"triggers": {
"default pageview": {
"on": "visible",
"request": "event",
"vars": {
"eventName": "page-loaded",
"eventId": "42"
}
}
}]
}
}
</script>
</amp-analytics>
Expand All @@ -48,29 +50,33 @@
"vars": {
"account": "UA-XXXX-Y"
},
"triggers": [{
"on": "visible",
"request": "pageview",
"vars": {
"title": "Example Pageview"
"triggers": {
"default pageview": {
"on": "visible",
"request": "pageview",
"vars": {
"title": "Example Pageview"
}
},
"click on #test1 trigger": {
"on": "click",
"selector": "#test1",
"request": "event",
"vars": {
"eventCategory": "examples",
"eventAction": "clicked-test1"
}
},
"click on #top trigger": {
"on": "click",
"selector": "#top",
"request": "event",
"vars": {
"eventCategory": "examples",
"eventAction": "clicked-header"
}
}
}, {
"on": "click",
"selector": "#test1",
"request": "event",
"vars": {
"eventCategory": "examples",
"eventAction": "clicked-test1"
}
}, {
"on": "click",
"selector": "#top",
"request": "event",
"vars": {
"eventCategory": "examples",
"eventAction": "clicked-header"
}
}]
}
}
</script>
</amp-analytics>
Expand Down
20 changes: 11 additions & 9 deletions extensions/amp-analytics/0.1/amp-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,24 @@ export class AmpAnalytics extends AMP.BaseElement {

this.generateRequests_();

if (!Array.isArray(this.config_['triggers'])) {
if (!this.config_['triggers']) {
log.error(this.getName_(), 'No triggers were found in the config. No ' +
'analytics data will be sent.');
return Promise.resolve();
}

// Trigger callback can be synchronous. Do the registration at the end.
for (let k = 0; k < this.config_['triggers'].length; k++) {
const trigger = this.config_['triggers'][k];
if (!trigger['on'] || !trigger['request']) {
log.warn(this.getName_(), '"on" and "request" attributes are ' +
'required for data to be collected.');
continue;
for (const k in this.config_['triggers']) {
if (this.config_['triggers'].hasOwnProperty(k)) {
const trigger = this.config_['triggers'][k];
if (!trigger['on'] || !trigger['request']) {
log.warn(this.getName_(), '"on" and "request" attributes are ' +
'required for data to be collected.');
continue;
}
addListener(this.getWin(), trigger['on'],
this.handleEvent_.bind(this, trigger), trigger['selector']);
}
addListener(this.getWin(), trigger['on'],
this.handleEvent_.bind(this, trigger), trigger['selector']);
}
return Promise.resolve();
}
Expand Down