feat: posthog telemetry

This commit is contained in:
Anoop M D 2022-10-30 17:48:36 +05:30
parent 820c99711b
commit f283df2a1b
4 changed files with 73 additions and 0 deletions

View File

@ -1,5 +1,8 @@
module.exports = {
reactStrictMode: true,
publicRuntimeConfig: {
CI: process.env.CI
},
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
if (!isServer) {

View File

@ -34,6 +34,8 @@
"nanoid": "3.3.4",
"next": "12.3.1",
"path": "^0.12.7",
"platform": "^1.3.6",
"posthog-node": "^2.1.0",
"qs": "^6.11.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",

View File

@ -1,5 +1,6 @@
import React, { useEffect } from 'react';
import useIdb from './useIdb';
import useTelemetry from './useTelemetry';
import useLocalCollectionTreeSync from './useLocalCollectionTreeSync';
import { useDispatch } from 'react-redux';
import { refreshScreenWidth } from 'providers/ReduxStore/slices/app';
@ -9,6 +10,7 @@ export const AppContext = React.createContext();
export const AppProvider = (props) => {
useIdb();
useTelemetry();
useLocalCollectionTreeSync();
const dispatch = useDispatch();

View File

@ -0,0 +1,66 @@
import { useEffect } from 'react';
import getConfig from 'next/config';
import { PostHog } from 'posthog-node';
import platformLib from 'platform';
import { uuid } from 'utils/common';
import { isElectron } from 'utils/common/platform';
const { publicRuntimeConfig } = getConfig();
const posthogApiKey = 'phc_7gtqSrrdZRohiozPMLIacjzgHbUlhalW1Bu16uYijMR';
let posthogClient = null;
const isCI = () => {
return publicRuntimeConfig.CI ? true : false;
};
// Todo support chrome and firefox extension
const getPlatform = () => {
return isElectron() ? 'electron' : 'web';
};
const getPosthogClient = () => {
if(posthogClient) {
return posthogClient;
}
posthogClient = new PostHog(posthogApiKey);
return posthogClient;
};
const getAnonymousTrackingId = () => {
let id = localStorage.getItem('bruno.anonymousTrackingId');
if(!id || !id.length || id.length !== 21) {
id = uuid();
localStorage.setItem('bruno.anonymousTrackingId', id);
}
return id;
};
const trackStart = () => {
if(isCI()) {
return;
}
const trackingId = getAnonymousTrackingId();
const platform = getPlatform();
const client = getPosthogClient();
client.capture({
distinctId: trackingId,
event: 'start',
properties: {
platform: platform,
os: platformLib.os.family
}
});
};
const useTelemetry = () => {
useEffect(() => {
trackStart();
setInterval(trackStart , 24 * 60 * 60 * 1000);
}, []);
};
export default useTelemetry;