Skip to content

Commit

Permalink
Add a pageVisibility test tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Feb 28, 2016
1 parent b0242a8 commit e6147de
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 47 deletions.
1 change: 1 addition & 0 deletions meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ host: ga-dev-tools.appspot.com
slug: ''
path: '/'
tracking_id: UA-41425441-5
tracking_id_testing: UA-41425441-7
client_id: 793177639245-olst43lspv93vkoql0b9l26hmpf9kfmv.apps.googleusercontent.com


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"license": "Apache-2.0",
"devDependencies": {
"autotrack": "^0.4.0",
"autotrack": "googleanalytics/autotrack#page-visibility-tracker",
"babel-core": "^6.1.13",
"babel-eslint": "^4.1.6",
"babel-loader": "^6.2.0",
Expand Down
55 changes: 27 additions & 28 deletions src/javascript/analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
// limitations under the License.


/* global ga */


import 'autotrack/lib/plugins/event-tracker';
import 'autotrack/lib/plugins/media-query-tracker';
import 'autotrack/lib/plugins/outbound-link-tracker';
import 'autotrack/lib/plugins/session-duration-tracker';
import 'autotrack/lib/plugins/page-visibility-tracker';


let mediaQueryDefinitions = [
Expand Down Expand Up @@ -55,34 +52,36 @@ let mediaQueryDefinitions = [
];


function setupUncaughtExceptionTracking() {
window.onerror = function(message, url, line, col) {
let desc = 'Uncaught error: ' +
message + ' (line: ' + line + ', url: ' + url + ', col: ' + col + ')';

ga('send', 'exception', {
exDescription: desc,
exFatal: false
});
};
}
/**
* Initializes all analytics.js tracking.
*/
export function init() {

// Requires official plugins
ga('require', 'displayfeatures');
ga('require', 'linkid');

export default {
track: function() {
// Requires autotrack plugins
ga('require', 'eventTracker');
ga('require', 'mediaQueryTracker', {mediaQueryDefinitions});
ga('require', 'outboundLinkTracker');

// Requires official plugins
ga('require', 'displayfeatures');
ga('require', 'linkid');
// Only requires pageVisibilityTracker on the testing tracker.
ga('testing.require', 'pageVisibilityTracker');

// Requires autotrack plugins
ga('require', 'eventTracker');
ga('require', 'mediaQueryTracker', {mediaQueryDefinitions});
ga('require', 'outboundLinkTracker');
ga('require', 'sessionDurationTracker');
ga('send', 'pageview');
}

ga('send', 'pageview');

setupUncaughtExceptionTracking();
/**
* Shadows the global `ga` command queue to allow for running commands on
* multiple test trackers.
* @param {string} command The command to run.
* @param {*} ...args A list of arguments to pass to the command queue.
*/
export function ga(command, ...args) {
window.ga(command, ...args);
if (!command.startsWith('testing.')) {
window.ga(`testing.${command}`, ...args);
}
};
}
5 changes: 4 additions & 1 deletion src/javascript/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.


/* global $, ga, gapi */
/* global $, gapi */


import {ga} from './analytics';


export default {
Expand Down
4 changes: 2 additions & 2 deletions src/javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import 'babel-polyfill';
import svg4everybody from 'svg4everybody/dist/svg4everybody';

import analytics from './analytics';
import * as analytics from './analytics';
import header from './header';
import highlighter from './highlighter';
import sidebar from './sidebar';
Expand All @@ -26,7 +26,7 @@ import sidebar from './sidebar';
svg4everybody();

// Setup Google Analytics tracking.
analytics.track();
analytics.init();

// Initiaze the header functionality.
header.init();
Expand Down
4 changes: 1 addition & 3 deletions src/javascript/query-explorer/components/query-explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// limitations under the License.


/* global ga */


import React from 'react';
import accountSummaries from 'javascript-api-utils/lib/account-summaries';

Expand All @@ -25,6 +22,7 @@ import QueryReport from './query-report';
import Select2MultiSuggest from './select2-multi-suggest';
import ViewSelector from './view-selector';

import {ga} from '../../analytics';
import AlertDispatcher from '../../components/alert-dispatcher';
import SearchSuggest from '../../components/search-suggest';

Expand Down
4 changes: 1 addition & 3 deletions src/javascript/query-explorer/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
// limitations under the License.


/* global ga */


import mapValues from 'lodash/mapValues';
import qs from 'querystring';
import {createStore, applyMiddleware} from 'redux';
import thunkMiddleware from 'redux-thunk';
import {sanitize} from './query-params';
import reducer from './reducers';
import {ga} from '../analytics';
import db from '../data-store';


Expand Down
5 changes: 4 additions & 1 deletion src/javascript/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// limitations under the License.


/* global $, ga */
/* global $ */


import {ga} from './analytics';


export default {
Expand Down
22 changes: 14 additions & 8 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@
{% block head_scripts %}
<script>
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
ga('create', '{{ site.tracking_id }}', 'auto', {siteSpeedSampleRate: 10});
</script>
<script async src="//www.google-analytics.com/analytics.js"></script>
{% if site.env != 'production' %}
<script>

ga('create', '{{ site.tracking_id }}', 'auto');
ga('create', '{{ site.tracking_id_testing }}', 'auto', 'testing');

{% if site.env != 'production' %}
ga('set', 'sendHitTask', function(model) {
//console.log(decodeURIComponent(model.get('hitPayload')).split('&'));
console.log('t0', Date.now(), decodeURIComponent(model.get('hitPayload')));
throw 'Abort tracking in non-production environments.'
});
</script>
{% endif %}
ga('testing.set', 'sendHitTask', function(model) {
console.log('testing', Date.now(), decodeURIComponent(model.get('hitPayload')));
throw 'Abort tracking in non-production environments.'
});
{% endif %}

</script>
<script async src="https://www.google-analytics.com/analytics.js"></script>
{% endblock %}
</head>
<body class="Site {{ initial_state }}">
Expand Down

0 comments on commit e6147de

Please sign in to comment.