Skip to content

Commit

Permalink
Backgrounded Variable added to Timer Trigger (#5240)
Browse files Browse the repository at this point in the history
* Added backgrounded variable to timer trigger.

* Inlined backgrounded field in vars object.

* Hoisted timer event function.

* Added BACKGROUNDED_STATE to url replacements.  Added support for BACKGROUNDED_STATE in amp-analytics.  Added documentation for BACKGROUNDED_STATE.  Added tests for BACKGROUNDED_STATE.

* Use correct set call for BACKGROUNDED_STATE call.

* Changed BACKGROUNDED_STATE to BACKGROUND_STATE

* Update instrumentation.js
  • Loading branch information
dynes authored and erwinmombay committed Oct 6, 2016
1 parent a85f316 commit 784d268
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 2 deletions.
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}&backgroundState=${backgroundState}"
},
"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
1 change: 1 addition & 0 deletions extensions/amp-analytics/0.1/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,4 @@ export function instrumentationServiceFor(window) {
return fromClass(window, 'amp-analytics-instrumentation',
InstrumentationService);
}

1 change: 1 addition & 0 deletions extensions/amp-analytics/0.1/vendors.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const ANALYTICS_CONFIG = /** @type {!JSONType} */ ({
'authdata': 'AUTHDATA',
'availableScreenHeight': 'AVAILABLE_SCREEN_HEIGHT',
'availableScreenWidth': 'AVAILABLE_SCREEN_WIDTH',
'backgroundState': 'BACKGROUND_STATE',
'browserLanguage': 'BROWSER_LANGUAGE',
'canonicalHost': 'CANONICAL_HOST',
'canonicalPath': 'CANONICAL_PATH',
Expand Down
7 changes: 7 additions & 0 deletions extensions/amp-analytics/analytics-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ Provides the number of seconds that have elapsed since 1970. (Epoch time)
Example value: `1452710304312`
### backgroundState
When used, will provide the current backgrounded state of the page.
Possible values are 0, the page is visible, or 1, the page is backgrounded.
## Visibility Variables
### backgrounded
Expand Down
6 changes: 6 additions & 0 deletions spec/amp-var-substitutions.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ Provides the total time the user has been enagaged with the page since the page
first became visible in the viewport. Total engaged time will be 0 until the
page first becomes visible. This variable requires the [amp-analytics](../extensions/amp-analytics/amp-analytics.md) extension to be present on the page.

### BACKGROUND_STATE

When used, will provide the current background state of the page.

Possible values are 0, the page is visible, or 1, the page is backgrounded.

## Access

Access variables are described in [amp-access-analytics.md](../extensions/amp-access/amp-access-analytics.md).
4 changes: 4 additions & 0 deletions src/service/url-replacements-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,10 @@ export class UrlReplacements {

// returns the AMP version number
this.set_('AMP_VERSION', () => '$internalRuntimeVersion$');

this.set_('BACKGROUND_STATE', () => {
return viewerForDoc(this.ampdoc.win.document).isVisible() ? '0' : '1';
});
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/functional/test-url-replacements.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,30 @@ describe('UrlReplacements', () => {
});
});

it('Should replace BACKGROUND_STATE with 0', () => {
const win = getFakeWindow();
win.services.viewer = {
obj: {isVisible: () => true},
};
return installUrlReplacementsServiceForDoc(win.ampdoc)
.expandAsync('?sh=BACKGROUND_STATE')
.then(res => {
expect(res).to.equal('?sh=0');
});
});

it('Should replace BACKGROUND_STATE with 1', () => {
const win = getFakeWindow();
win.services.viewer = {
obj: {isVisible: () => false},
};
return installUrlReplacementsServiceForDoc(win.ampdoc)
.expandAsync('?sh=BACKGROUND_STATE')
.then(res => {
expect(res).to.equal('?sh=1');
});
});

describe('PAGE_LOAD_TIME', () => {
let win;
let ampdoc;
Expand Down

0 comments on commit 784d268

Please sign in to comment.