UNPKG

1.1 kBJavaScriptView Raw
1/**
2 * SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3 * SPDX-License-Identifier: LGPL-3.0-or-later
4 */
5
6import Insight from 'insight';
7import path from 'path';
8import readJsonSync from 'read-json-sync';
9
10const GA_TOKEN = 'UA-37033501-13';
11
12export let PROJECT_NAME;
13export let PROJECT_VERSION;
14
15let insight;
16
17/**
18 * Initialize insight facility
19 * @return {Promise} a promise fulfilled when initialization is done
20 */
21export function init() {
22 return new Promise(resolve => {
23 try {
24 const projectPkgJson = readJsonSync(
25 path.join(process.cwd(), 'package.json')
26 );
27
28 PROJECT_NAME = projectPkgJson.name;
29 PROJECT_VERSION = projectPkgJson.version;
30
31 insight = new Insight({
32 trackingCode: GA_TOKEN,
33 pkg: require('../package.json'),
34 });
35 } catch (err) {
36 // ignore
37 }
38
39 if (insight && insight.optOut === undefined) {
40 insight.askPermission(undefined, resolve);
41 } else {
42 resolve();
43 }
44 });
45}
46
47export const track = (...args) => {
48 if (insight) {
49 insight.track(
50 insight.config.get('clientId'),
51 insight.PROJECT_NAME,
52 ...args
53 );
54 }
55};