Skip to content

Commit

Permalink
feat(stats): Intent to ship stats
Browse files Browse the repository at this point in the history
Implement stats collector.

Fix #928
  • Loading branch information
netil committed Jun 12, 2019
1 parent 1b524e1 commit bc163b9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/config/Options.js
Expand Up @@ -86,6 +86,22 @@ export default class Options {
size_width: undefined,
size_height: undefined,

/**
* Allow usage stats collection.
* - **NOTE:**
* - The usage stats collection is used for reference purpose only.
* - The stats data will be sent in a period of once in every 2 weeks.
* - Help us to make a better chart library! :)
* @name stats
* @memberof Options
* @type {Boolean}
* @default true
* @example
* // turn off stats sending
* stats: false
*/
stats: true,

/**
* The padding of the chart element.
* @name padding
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Expand Up @@ -115,7 +115,7 @@ const bb = {
},

/**
* Set or get default options globally.
* Set or get global default options.
* - **NOTE:**
* - The options values settings are valid within page context only.
* - If is called multiple times, will override the last value.
Expand Down
3 changes: 2 additions & 1 deletion src/internals/ChartInternal.js
Expand Up @@ -17,7 +17,7 @@ import {transition as d3Transition} from "d3-transition";
import Axis from "../axis/Axis";
import CLASS from "../config/classes";
import {isMobile} from "../internals/browser";
import {notEmpty, asHalfPixel, getOption, isValue, isArray, isFunction, isString, isNumber, isObject, callFn, sortValue} from "./util";
import {notEmpty, asHalfPixel, getOption, isValue, isArray, isFunction, isString, isNumber, isObject, callFn, sendStats, sortValue} from "./util";

/**
* Internal chart class.
Expand All @@ -40,6 +40,7 @@ export default class ChartInternal {
beforeInit() {
const $$ = this;

$$.config.stats && sendStats();
$$.callPluginHook("$beforeInit");

// can do something
Expand Down
32 changes: 32 additions & 0 deletions src/internals/util.js
Expand Up @@ -81,6 +81,7 @@ const sanitise = str => (isString(str) ? str.replace(/</g, "&lt;").replace(/>/g,
* @param {d3Selection} node Text node
* @param {String} text Text value string
* @param {Array} dy dy value for multilined text
* @private
*/
const setTextValue = (node, text, dy = [-1, 1]) => {
if (!node || !isString(text)) {
Expand Down Expand Up @@ -327,6 +328,36 @@ const getRange = (start, end) => {
return res;
};

/**
* Send stats
* @private
*/
const sendStats = () => {
if (navigator && localStorage) {
const key = "$bb.stats";
const url = `https://www.google-analytics.com/collect?v=1&tid=UA-141911582-1&cid=555&t=pageview&dp=%2F${location ? location.hostname : ""}`;
const t = +new Date();
const last = +localStorage.getItem(key);
const expire = 1000 * 60 * 60 * 24 * 14;

if (!last || (last + expire) < t) {
localStorage.setItem(key, t + expire);

if (navigator.sendBeacon) {
navigator.sendBeacon(url);
} else {
const i = new Image();

i.src = url;
i.style.display = "none";

document.body.appendChild(i);
document.body.removeChild(i);
}
}
}
};

// emulate event
const emulateEvent = {
mouse: (() => {
Expand Down Expand Up @@ -434,6 +465,7 @@ export {
mergeObj,
notEmpty,
sanitise,
sendStats,
setTextValue,
sortValue,
toArray,
Expand Down
2 changes: 1 addition & 1 deletion types/bb.d.ts
Expand Up @@ -13,7 +13,7 @@ export const bb: {
generate(options: ChartOptions): Chart;

/**
* Set or get default options globally.
* Set or get global default options.
* - **NOTE:**
* - The options values settings are valid within page context only.
* - If is called multiple times, will override the last value.
Expand Down
9 changes: 9 additions & 0 deletions types/options.d.ts
Expand Up @@ -40,6 +40,15 @@ export interface ChartOptions {
height?: number;
};

/**
* Allow usage stats collection.
* - **NOTE:**
* - The usage stats collection is used for reference purpose only.
* - The stats data will be sent in a period of once in every 2 weeks.
* - Help us to make a better chart library! :)
*/
stats?: boolean;

padding?: {
/**
* The padding on the top of the chart.
Expand Down

0 comments on commit bc163b9

Please sign in to comment.