This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup usage tracking and add documentation
- Loading branch information
Showing
6 changed files
with
196 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Collecting usage data | ||
|
||
You can choose to have the Prototype Kit send anonymous usage data for analysis. | ||
This helps the team working on the Kit understand how it's being used, in order | ||
to improve it - please don't turn off usage data unless you have to. | ||
|
||
## How it works | ||
|
||
When you first run the Prototype Kit, it will ask you for permission to send | ||
usage data to Google Analytics. It will store your answer in `usage-data-config.json` and it won't ask | ||
you again. | ||
|
||
If you say yes, it will store an anonymous, unique ID number in `usage-data-config.json`. | ||
|
||
## Data we collect | ||
|
||
Whenever you start the Prototype Kit, it will send: | ||
|
||
- your anonymous ID number | ||
- the Prototype Kit version number | ||
- your operating system (for example 'Windows 10') | ||
- your Node.js version | ||
|
||
## Change usage data settings | ||
|
||
You can start or stop sending usage data at any time. Delete `usage-data-config.json` | ||
and restart the Prototype Kit. It will ask you again whether you'd like to send data. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
Help us improve the GOV.UK Prototype Kit | ||
──────────────────────────────────────── | ||
|
||
With your permission, the kit can send useful anonymous usage data | ||
for analysis to help the team improve the service. Read more here: | ||
https://govuk-prototype-kit-beta.herokuapp.com/docs/usage-data | ||
|
||
Do you give permission for the kit to send anonymous usage data? (y/n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Core dependencies | ||
const path = require('path') | ||
const fs = require('fs') | ||
const os = require('os') | ||
|
||
// NPM dependencies | ||
const prompt = require('prompt') | ||
const universalAnalytics = require('universal-analytics') | ||
const uuidv4 = require('uuid/v4') | ||
|
||
// Local dependencies | ||
const packageJson = require('../package.json') | ||
|
||
exports.getUsageDataConfig = function () { | ||
// Try to read config file to see if usage data is opted in | ||
let usageDataConfig = {} | ||
try { | ||
usageDataConfig = require(path.join(__dirname, '../usage-data-config.json')) | ||
} catch (e) { | ||
// do nothing - we will make a config | ||
} | ||
return usageDataConfig | ||
} | ||
|
||
exports.setUsageDataConfig = function (usageDataConfig) { | ||
const usageDataConfigJSON = JSON.stringify(usageDataConfig, null, ' ') | ||
try { | ||
fs.writeFileSync(path.join(__dirname, '../usage-data-config.json'), usageDataConfigJSON) | ||
return true | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
return false | ||
} | ||
|
||
// Ask for permission to track data | ||
// returns a Promise with the user's answer | ||
exports.askForUsageDataPermission = function () { | ||
return new Promise(function (resolve, reject) { | ||
// Set up prompt settings | ||
prompt.colors = false | ||
prompt.start() | ||
prompt.message = '' | ||
prompt.delimiter = '' | ||
|
||
const description = fs.readFileSync(path.join(__dirname, 'usage-data-prompt.txt'), 'utf8') | ||
|
||
prompt.get([{ | ||
name: 'answer', | ||
description: description, | ||
required: true, | ||
type: 'string', | ||
pattern: /y(es)?|no?/i, | ||
message: 'Please enter y or n' | ||
}], function (err, result) { | ||
if (err) { | ||
reject(err) | ||
} | ||
if (result.answer.match(/y(es)?/i)) { | ||
resolve('yes') | ||
} else { | ||
resolve('no') | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
exports.startTracking = function (usageDataConfig) { | ||
// Get client ID for tracking | ||
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#cid | ||
|
||
if (usageDataConfig.clientId === undefined) { | ||
usageDataConfig.clientId = uuidv4() | ||
exports.setUsageDataConfig(usageDataConfig) | ||
} | ||
|
||
// Track kit start event, with kit version, operating system and Node.js version | ||
const trackingId = 'UA-2889574-13' | ||
const trackingUser = universalAnalytics(trackingId, usageDataConfig.clientId) | ||
|
||
const kitVersion = packageJson.version | ||
const operatingSystem = os.platform() + ' ' + os.release() | ||
const nodeVersion = process.versions.node | ||
|
||
// Anonymise the IP | ||
trackingUser.set('anonymizeIp', 1) | ||
|
||
// Set custom dimensions | ||
trackingUser.set('cd1', operatingSystem) | ||
trackingUser.set('cd2', kitVersion) | ||
trackingUser.set('cd3', nodeVersion) | ||
|
||
// Trigger start event | ||
trackingUser.event('State', 'Start').send() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters