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

Backgrounded Variable added to Timer Trigger #5240

Merged
merged 12 commits into from
Oct 6, 2016
Merged
5 changes: 3 additions & 2 deletions examples/analytics.amp.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"endpoint": "https://raw.githubusercontent.com/ampproject/amphtml/master/examples/img/ampicon.png",
"base": "${endpoint}?${type}&path=${canonicalPath}",
"event": "${base}&scrollY=${scrollTop}&scrollX=${scrollLeft}&height=${availableScreenHeight}&width=${availableScreenWidth}&scrollBoundV=${verticalScrollBoundary}&scrollBoundH=${horizontalScrollBoundary}",
"visibility": "${base}&a=${maxContinuousVisibleTime}&b=${totalVisibleTime}&c=${firstSeenTime}&d=${lastSeenTime}&e=${fistVisibleTime}&f=${lastVisibleTime}&g=${minVisiblePercentage}&h=${maxVisiblePercentage}&i=${elementX}&j=${elementY}&k=${elementWidth}&l=${elementHeight}&m=${totalTime}&n=${loadTimeVisibility}&o=${backgroundedAtStart}&p=${backgrounded}&subTitle=${subTitle}"
"visibility": "${base}&a=${maxContinuousVisibleTime}&b=${totalVisibleTime}&c=${firstSeenTime}&d=${lastSeenTime}&e=${fistVisibleTime}&f=${lastVisibleTime}&g=${minVisiblePercentage}&h=${maxVisiblePercentage}&i=${elementX}&j=${elementY}&k=${elementWidth}&l=${elementHeight}&m=${totalTime}&n=${loadTimeVisibility}&o=${backgroundedAtStart}&p=${backgrounded}&subTitle=${subTitle}",
"timer": "${base}&backgrounded=${backgrounded}"
},
"vars": {
"title": "Example Request"
Expand Down Expand Up @@ -111,7 +112,7 @@
"interval": 5,
"maxTimerLength": 20
},
"request": "base",
"request": "timer",
"vars": {
"type": "timer"
}
Expand Down
15 changes: 10 additions & 5 deletions extensions/amp-analytics/0.1/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,17 @@ export class InstrumentationService {
createTimerListener_(listener, timerSpec) {
const hasImmediate = timerSpec.hasOwnProperty('immediate');
const callImmediate = hasImmediate ? Boolean(timerSpec.immediate) : true;
const intervalId = this.win_.setInterval(
listener.bind(null, new AnalyticsEvent(AnalyticsEventType.TIMER)),
timerSpec['interval'] * 1000
);
const viewer = this.viewer_;
const intervalId = this.win_.setInterval(() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hoist this function instead of passing it. Then we can call it in the callImmediate conditional below.

const vars = {};
vars['backgrounded'] = viewer.isVisible() ? '0' : '1';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be done inline:

const vars = {
  backgrounded: viewer.isVisible ? '0' : '1',
};

listener(new AnalyticsEvent(AnalyticsEventType.TIMER, vars));
}, timerSpec['interval'] * 1000);

if (callImmediate) {
listener(new AnalyticsEvent(AnalyticsEventType.TIMER));
const vars = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could instead call the function above directly.

vars['backgrounded'] = viewer.isVisible() ? '0' : '1';
listener(new AnalyticsEvent(AnalyticsEventType.TIMER, vars));
}

const maxTimerLength = timerSpec['maxTimerLength'] ||
Expand All @@ -516,3 +520,4 @@ export function instrumentationServiceFor(window) {
return fromClass(window, 'amp-analytics-instrumentation',
InstrumentationService);
}