Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

add basic telemetry

ammarahm-ed3cc01e5e4aad
diff --git a/apps/mobile/App.js b/apps/mobile/App.js
index 21c9951c2..dfb7e0ada 100644
--- a/apps/mobile/App.js
+++ b/apps/mobile/App.js
@@ -1,29 +1,30 @@
import http from 'notes-core/utils/http';
-import React, { useEffect } from 'react';
+import React, {useEffect} from 'react';
import Orientation from 'react-native-orientation';
-import { SafeAreaProvider } from 'react-native-safe-area-context';
+import {SafeAreaProvider} from 'react-native-safe-area-context';
import SplashScreen from 'react-native-splash-screen';
-import { AppRootEvents } from './AppRootEvents';
-import { RootView } from './initializer.root';
+import {AppRootEvents} from './AppRootEvents';
+import {RootView} from './initializer.root';
import AppLoader from './src/components/AppLoader';
-import { useTracked } from './src/provider';
+import {useTracked} from './src/provider';
import {
initialize,
useMessageStore,
useNoteStore,
useSettingStore,
- useUserStore
+ useUserStore,
} from './src/provider/stores';
-import { DDS } from './src/services/DeviceDetection';
+import {DDS} from './src/services/DeviceDetection';
import {
eSendEvent,
eSubscribeEvent,
- eUnSubscribeEvent
+ eUnSubscribeEvent,
} from './src/services/EventManager';
import SettingsService from './src/services/SettingsService';
-import { db } from './src/utils/DB';
-import { eDispatchAction } from './src/utils/Events';
-import { MMKV } from './src/utils/mmkv';
+import {Tracker} from './src/utils';
+import {db} from './src/utils/DB';
+import {eDispatchAction} from './src/utils/Events';
+import {MMKV} from './src/utils/mmkv';
import EditorRoot from './src/views/Editor/EditorRoot';
let databaseHasLoaded = false;
@@ -32,9 +33,7 @@ async function loadDefaultNotes() {
try {
const isCreated = await MMKV.getItem('defaultNoteCreated');
if (isCreated) return;
- const notes = await http.get(
- 'https://app.notesnook.com/notes/index.json',
- );
+ const notes = await http.get('https://app.notesnook.com/notes/index.json');
if (!notes) return;
for (let note of notes) {
const content = await http.get(note.mobileContent);
@@ -69,14 +68,18 @@ const loadDatabase = async () => {
function checkOrientation() {
Orientation.getOrientation((e, r) => {
DDS.checkSmallTab(r);
- useSettingStore.getState().setDimensions({width:DDS.width,height:DDS.height});
- useSettingStore.getState().setDeviceMode(
- DDS.isLargeTablet()
- ? 'tablet'
- : DDS.isSmallTab
- ? 'smallTablet'
- : 'mobile',
- );
+ useSettingStore
+ .getState()
+ .setDimensions({width: DDS.width, height: DDS.height});
+ useSettingStore
+ .getState()
+ .setDeviceMode(
+ DDS.isLargeTablet()
+ ? 'tablet'
+ : DDS.isSmallTab
+ ? 'smallTablet'
+ : 'mobile',
+ );
});
}
@@ -106,7 +109,15 @@ const App = () => {
setVerifyUser(true);
}
await loadDatabase();
- } catch (e) {} finally {
+
+ if (!SettingsService.get().hasOwnProperty('telemetry')) {
+ await SettingsService.set('telemetry', true);
+ }
+ if (SettingsService.get().telemetry) {
+ Tracker.record('50bf361f-dba0-41f1-9570-93906249a6d3');
+ }
+ } catch (e) {
+ } finally {
databaseHasLoaded = true;
loadMainApp();
}
diff --git a/apps/mobile/src/provider/interfaces.ts b/apps/mobile/src/provider/interfaces.ts
index 9fd94feac..27680921b 100644
--- a/apps/mobile/src/provider/interfaces.ts
+++ b/apps/mobile/src/provider/interfaces.ts
@@ -73,6 +73,7 @@ export type Settings = {
screenshotMode?: boolean,
privacyScreen?: boolean,
appLockMode?: string,
+ telemetry:boolean
}
type Dimensions = {
diff --git a/apps/mobile/src/provider/stores.ts b/apps/mobile/src/provider/stores.ts
index 682d01f83..229ee973d 100644
--- a/apps/mobile/src/provider/stores.ts
+++ b/apps/mobile/src/provider/stores.ts
@@ -164,6 +164,7 @@ export const useSettingStore = create<SettingStore>((set, get) => ({
screenshotMode: true,
privacyScreen: false,
appLockMode: 'none',
+ telemetry:true,
},
fullscreen: false,
deviceMode: null,
diff --git a/apps/mobile/src/services/SettingsService.js b/apps/mobile/src/services/SettingsService.js
index 6ac3306cc..29c50faf6 100644
--- a/apps/mobile/src/services/SettingsService.js
+++ b/apps/mobile/src/services/SettingsService.js
@@ -22,7 +22,8 @@ export const defaultSettings = {
sortOrder: 'desc',
screenshotMode: true,
privacyScreen: false,
- appLockMode: 'none',
+ appLockMode: 'none',
+ telemetry:false
}
let settings = {...defaultSettings}
diff --git a/apps/mobile/src/utils/ackee.js b/apps/mobile/src/utils/ackee.js
new file mode 100644
index 000000000..e2453269e
--- /dev/null
+++ b/apps/mobile/src/utils/ackee.js
@@ -0,0 +1,353 @@
+import {AppState, Platform} from 'react-native';
+
+const platform = {
+ osName: Platform.OS,
+ osVersion: Platform.Version,
+};
+
+const isBrowser = typeof window !== 'undefined';
+
+/**
+ * Validates options and sets defaults for undefined properties.
+ * @param {?Object} opts
+ * @returns {Object} opts - Validated options.
+ */
+const validate = function (opts = {}) {
+ // Create new object to avoid changes by reference
+ const _opts = {};
+
+ // Defaults to false
+ _opts.detailed = opts.detailed === true;
+
+ // Defaults to true
+ _opts.ignoreLocalhost = opts.ignoreLocalhost !== false;
+
+ // Defaults to true
+ _opts.ignoreOwnVisits = opts.ignoreOwnVisits !== false;
+
+ return _opts;
+};
+
+/**
+ * Determines if a host is a localhost.
+ * @param {String} hostname - Hostname that should be tested.
+ * @returns {Boolean} isLocalhost
+ */
+const isLocalhost = function (hostname) {
+ return __DEV__;
+};
+
+/**
+ * Determines if user agent is a bot. Approach is to get most bots, assuming other bots don't run JS.
+ * Source: https://stackoverflow.com/questions/20084513/detect-search-crawlers-via-javascript/20084661
+ * @param {String} userAgent - User agent that should be tested.
+ * @returns {Boolean} isBot
+ */
+const isBot = function (userAgent) {
+ return /bot|crawler|spider|crawling/i.test(userAgent);
+};
+
+/**
+ * Checks if an id is a fake id. This is the case when Ackee ignores you because of the `ackee_ignore` cookie.
+ * @param {String} id - Id that should be tested.
+ * @returns {Boolean} isFakeId
+ */
+const isFakeId = function (id) {
+ return id === '88888888-8888-8888-8888-888888888888';
+};
+
+/**
+ * Checks if the website is in background (e.g. user has minimzed or switched tabs).
+ * @returns {boolean}
+ */
+const isInBackground = function () {
+ return AppState.currentState === 'background';
+};
+
+/**
+ * Get the optional source parameter.
+ * @returns {String} source
+ */
+const source = function () {
+ return undefined;
+};
+
+/**
+ * Gathers all platform-, screen- and user-related information.
+ * @param {Boolean} detailed - Include personal data.
+ * @returns {Object} attributes - User-related information.
+ */
+export const attributes = function (detailed = false) {
+ const defaultData = {
+ siteLocation: 'com.streetwriters.notesnook',
+ siteReferrer: '',
+ source: source(),
+ osName: platform.osName,
+ };
+
+ return {
+ ...defaultData,
+ };
+};
+
+/**
+ * Creates an object with a query and variables property to create a record on the server.
+ * @param {String} domainId - Id of the domain.
+ * @param {Object} input - Data that should be transferred to the server.
+ * @returns {Object} Create record body.
+ */
+const createRecordBody = function (domainId, input) {
+ return {
+ query: `
+ mutation createRecord($domainId: ID!, $input: CreateRecordInput!) {
+ createRecord(domainId: $domainId, input: $input) {
+ payload {
+ id
+ }
+ }
+ }
+ `,
+ variables: {
+ domainId,
+ input,
+ },
+ };
+};
+
+/**
+ * Creates an object with a query and variables property to update a record on the server.
+ * @param {String} recordId - Id of the record.
+ * @returns {Object} Update record body.
+ */
+const updateRecordBody = function (recordId) {
+ return {
+ query: `
+ mutation updateRecord($recordId: ID!) {
+ updateRecord(id: $recordId) {
+ success
+ }
+ }
+ `,
+ variables: {
+ recordId,
+ },
+ };
+};
+
+/**
+ * Creates an object with a query and variables property to create an action on the server.
+ * @param {String} eventId - Id of the event.
+ * @param {Object} input - Data that should be transferred to the server.
+ * @returns {Object} Create action body.
+ */
+const createActionBody = function (eventId, input) {
+ return {
+ query: `
+ mutation createAction($eventId: ID!, $input: CreateActionInput!) {
+ createAction(eventId: $eventId, input: $input) {
+ payload {
+ id
+ }
+ }
+ }
+ `,
+ variables: {
+ eventId,
+ input,
+ },
+ };
+};
+
+/**
+ * Creates an object with a query and variables property to update an action on the server.
+ * @param {String} actionId - Id of the action.
+ * @param {Object} input - Data that should be transferred to the server.
+ * @returns {Object} Update action body.
+ */
+const updateActionBody = function (actionId, input) {
+ return {
+ query: `
+ mutation updateAction($actionId: ID!, $input: UpdateActionInput!) {
+ updateAction(id: $actionId, input: $input) {
+ success
+ }
+ }
+ `,
+ variables: {
+ actionId,
+ input,
+ },
+ };
+};
+
+/**
+ * Construct URL to the GraphQL endpoint of Ackee.
+ * @param {String} server - URL of the Ackee server.
+ * @returns {String} endpoint - URL to the GraphQL endpoint of the Ackee server.
+ */
+const endpoint = function (server) {
+ const hasTrailingSlash = server.substr(-1) === '/';
+
+ return server + (hasTrailingSlash === true ? '' : '/') + 'api';
+};
+
+/**
+ * Sends a request to a specified URL.
+ * Won't catch all errors as some are already logged by the browser.
+ * In this case the callback won't fire.
+ * @param {String} url - URL to the GraphQL endpoint of the Ackee server.
+ * @param {Object} body - JSON which will be send to the server.
+ * @param {Object} opts - Options.
+ * @param {?Function} next - The callback that handles the response. Receives the following properties: json.
+ */
+const send = function (url, body, opts, next) {
+ const xhr = new XMLHttpRequest();
+
+ xhr.open('POST', url);
+
+ xhr.onload = () => {
+ if (xhr.status !== 200) {
+ throw new Error('Server returned with an unhandled status');
+ }
+
+ let json = null;
+
+ try {
+ json = JSON.parse(xhr.responseText);
+ } catch (e) {
+ throw new Error('Failed to parse response from server');
+ }
+
+ if (json.errors != null) {
+ throw new Error(json.errors[0].message);
+ }
+
+ if (typeof next === 'function') {
+ return next(json);
+ }
+ };
+
+ xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
+ xhr.withCredentials = opts.ignoreOwnVisits;
+
+ xhr.send(JSON.stringify(body));
+};
+
+/**
+ * Creates a new instance.
+ * @param {String} server - URL of the Ackee server.
+ * @param {?Object} opts
+ * @returns {Object} instance
+ */
+export const create = function (server, opts) {
+ opts = validate(opts);
+ const url = endpoint(server);
+ const noop = () => {};
+
+ // Fake instance when Ackee ignores you
+ const fakeInstance = {
+ record: () => ({stop: noop}),
+ updateRecord: () => ({stop: noop}),
+ action: noop,
+ updateAction: noop,
+ };
+
+ if (opts.ignoreLocalhost === true && isLocalhost() === true) {
+ console.warn('Ackee ignores you because you are on localhost');
+ return fakeInstance;
+ }
+ // Creates a new record on the server and updates the record
+ // very x seconds to track the duration of the visit. Tries to use
+ // the default attributes when there're no custom attributes defined.
+ const _record = (domainId, attrs = attributes(opts.detailed), next) => {
+ // Function to stop updating the record
+ let isStopped = false;
+ const stop = () => {
+ isStopped = true;
+ };
+
+ send(url, createRecordBody(domainId, attrs), opts, json => {
+ const recordId = json.data.createRecord.payload.id;
+
+ if (isFakeId(recordId) === true) {
+ return console.warn('Ackee ignores you because this is your own site');
+ }
+
+ const interval = setInterval(() => {
+ if (isStopped === true) {
+ clearInterval(interval);
+ return;
+ }
+
+ if (isInBackground() === true) return;
+
+ send(url, updateRecordBody(recordId), opts);
+ }, 15000);
+
+ if (typeof next === 'function') {
+ return next(recordId);
+ }
+ });
+
+ return {stop};
+ };
+
+ // Updates a record very x seconds to track the duration of the visit
+ const _updateRecord = recordId => {
+ // Function to stop updating the record
+ let isStopped = false;
+ const stop = () => {
+ isStopped = true;
+ };
+
+ if (isFakeId(recordId) === true) {
+ console.warn('Ackee ignores you because this is your own site');
+ return {stop};
+ }
+
+ const interval = setInterval(() => {
+ if (isStopped === true) {
+ clearInterval(interval);
+ return;
+ }
+
+ if (isInBackground() === true) return;
+
+ send(url, updateRecordBody(recordId), opts);
+ }, 15000);
+
+ return {stop};
+ };
+
+ // Creates a new action on the server
+ const _action = (eventId, attrs, next) => {
+ send(url, createActionBody(eventId, attrs), opts, json => {
+ const actionId = json.data.createAction.payload.id;
+
+ if (isFakeId(actionId) === true) {
+ return console.warn('Ackee ignores you because this is your own site');
+ }
+
+ if (typeof next === 'function') {
+ return next(actionId);
+ }
+ });
+ };
+
+ // Updates an action
+ const _updateAction = (actionId, attrs) => {
+ if (isFakeId(actionId) === true) {
+ return console.warn('Ackee ignores you because this is your own site');
+ }
+
+ send(url, updateActionBody(actionId, attrs), opts);
+ };
+
+ // Return the real instance
+ return {
+ record: _record,
+ updateRecord: _updateRecord,
+ action: _action,
+ updateAction: _updateAction,
+ };
+};
diff --git a/apps/mobile/src/utils/index.js b/apps/mobile/src/utils/index.js
index e3ddbd063..5eee15234 100755
--- a/apps/mobile/src/utils/index.js
+++ b/apps/mobile/src/utils/index.js
@@ -15,6 +15,12 @@ import { refreshNotesPage } from './Events';
import {MMKV} from './mmkv';
import {tabBarRef} from './Refs';
import {SIZE} from './SizeUtils';
+import * as ackeeTracker from "./ackee"
+export const Tracker = ackeeTracker.create('https://sa.streetwriters.co',{
+ ignoreLocalhost:true
+});
+
+
const imgNames = [
'favorites',
diff --git a/apps/mobile/src/views/Settings/index.js b/apps/mobile/src/views/Settings/index.js
index fa3aa67b3..c14c69203 100644
--- a/apps/mobile/src/views/Settings/index.js
+++ b/apps/mobile/src/views/Settings/index.js
@@ -288,7 +288,12 @@ export const Settings = ({navigation}) => {
paddingHorizontal: 0,
}}>
{!DDS.isTab && (
- <Header noAnnouncement={true} title="Settings" type="settings" messageCard={false} />
+ <Header
+ noAnnouncement={true}
+ title="Settings"
+ type="settings"
+ messageCard={false}
+ />
)}
<SettingsUserSection />
@@ -1403,6 +1408,23 @@ const SettingsPrivacyAndSecurity = () => {
)}
<SectionHeader title="Privacy & Security" />
+ <CustomButton
+ key="telemetry"
+ title="Enable telemetry"
+ tagline="Usage data & crash reports will be sent to us (no 3rd party involved) for analytics. All data is anonymous as mentioned in our privacy policy."
+ onPress={() => {
+ SettingsService.set('telemetry', !settings.telemetry);
+ }}
+ maxWidth="90%"
+ customComponent={
+ <Icon
+ size={SIZE.xl}
+ color={settings.telemetry ? colors.accent : colors.icon}
+ name={settings.telemetry ? 'toggle-switch' : 'toggle-switch-off'}
+ />
+ }
+ />
+
<CustomButton
key="privacyMode"
title="Privacy mode"