Skip to content

Commit

Permalink
Merge pull request #206 from tonkeeper/feature/amplitude-implementation
Browse files Browse the repository at this point in the history
feature: implement amplitude
  • Loading branch information
voloshinskii authored Jan 10, 2023
2 parents c005c2e + 8fbc3cc commit 69fc360
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"icons": "node scripts/generate_icons.js"
},
"dependencies": {
"@amplitude/analytics-browser": "^1.6.7",
"@gorhom/bottom-sheet": "^4.4.4",
"@react-native-async-storage/async-storage": "^1.15.5",
"@react-native-community/clipboard": "^1.5.1",
Expand Down Expand Up @@ -75,7 +76,6 @@
"query-string": "^7.0.1",
"react": "17.0.2",
"react-native": "0.68.2",
"react-native-appsflyer": "6.5.21",
"react-native-camera": "^4.2.1",
"react-native-console-time-polyfill": "^1.2.3",
"react-native-crypto": "^2.2.0",
Expand Down
38 changes: 3 additions & 35 deletions src/core/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,17 @@ import React, { FC, useEffect } from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { Provider as StoreProvider, useSelector } from 'react-redux';
import { ThemeProvider } from 'styled-components/native';
import appsFlyer from 'react-native-appsflyer';

import { store } from '$store';
import { AppearanceAccents, DarkTheme, TonTheme } from '$styled';
import { AppNavigator } from '$navigation/AppNavigator';
import { Toast, ScrollPositionProvider } from '$uikit';
import { trackEvent } from '$utils';
import { trackEvent, trackFirstLaunch } from '$utils';
import { useMemo } from 'react';
import { accentSelector } from '$store/main';
import { ToastComponent } from '$uikit/Toast/new/ToastComponent';
import { View } from 'react-native';

let onInstallConversionDataCanceller: any = appsFlyer.onInstallConversionData((res) => {
if (JSON.parse(res.data.is_first_launch) === true) {
trackEvent('first_launch');
if (res.data.af_status === 'Non-organic') {
const media_source = res.data.media_source;
const campaign = res.data.campaign;
trackEvent('non_organic_install', {
media_source,
campaign,
});
} else if (res.data.af_status === 'Organic') {
trackEvent('organic_install');
}
}
});

let onAppOpenAttributionCanceller: any = appsFlyer.onAppOpenAttribution((res) => {
console.log('Appsflyer', res);
});

const TonThemeProvider: FC = ({ children }) => {
const accent = useSelector(accentSelector);

Expand All @@ -56,24 +35,13 @@ const TonThemeProvider: FC = ({ children }) => {
);
};

trackFirstLaunch();

export const App: FC = () => {
useEffect(() => {
trackEvent('launch_app');
}, []);

useEffect(() => {
return () => {
if (onInstallConversionDataCanceller) {
onInstallConversionDataCanceller();
onInstallConversionDataCanceller = null;
}
if (onAppOpenAttributionCanceller) {
onAppOpenAttributionCanceller();
onAppOpenAttributionCanceller = null;
}
};
});

return (
<StoreProvider {...{ store }}>
<TonThemeProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/core/ModalContainer/ExchangeMethod/ExchangeMethod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const ExchangeMethod: FC<ExchangeMethodProps> = ({ methodId, onContinue }
if (!wallet) {
return openRequireWalletModal();
} else {
trackEvent(`exchange_open_${methodId}`, { internal_id: methodId });
trackEvent(`exchange_open`, { internal_id: methodId });

if (onContinue) {
onContinue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const MarketplaceItem: FC<MarketplaceItemProps> = ({
const handlePress = useCallback(async () => {
await onPress?.();
openDAppBrowser(marketplaceUrl);
trackEvent(`marketplace_open_${internalId}`, { internal_id: internalId });
trackEvent(`marketplace_open`, { internal_id: internalId });
}, [marketplaceUrl, onPress, internalId]);

return (
Expand Down
2 changes: 2 additions & 0 deletions src/shared/constants/serverConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface ServerConfig {
cachedMediaSalt: string;
NFTOnExplorerUrl: string;
flags: Record<string, boolean>;
amplitudeKey: string;
}

let config: ServerConfig | null = null;
Expand Down Expand Up @@ -49,6 +50,7 @@ export function setServerConfig(data: any, isTestnet: boolean) {
cachedMediaSalt: data.cachedMediaSalt,
NFTOnExplorerUrl: data.NFTOnExplorerUrl || 'https://tonscan.org/nft/%s',
flags: data.flags || {},
amplitudeKey: data.amplitudeKey,
};
}

Expand Down
48 changes: 26 additions & 22 deletions src/utils/stats.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import appsFlyer from 'react-native-appsflyer';
import { debugLog } from '$utils';
import { getServerConfig } from '$shared/constants';
import { init, logEvent } from '@amplitude/analytics-browser';
import AsyncStorage from '@react-native-async-storage/async-storage';

let TrakingEnabled = false;
export function initStats() {
appsFlyer.anonymizeUser(true);

const options = {
devKey: getServerConfig('appsflyerDevKey'),
appId: getServerConfig('appsflyerAppId'),
onInstallConversionData: true,
isDebug: false,
};

appsFlyer.initSdk(
options,
(result) => {
console.log('AppsFlyer', result);
},
(error) => {
debugLog('AppsFlyer', error);
},
);

init(getServerConfig('amplitudeKey'), '-', {
minIdLength: 1,
deviceId: '-',
trackingOptions: {
ipAddress: false,
deviceModel: true,
language: false,
osName: true,
osVersion: true,
platform: true,
adid: false,
carrier: false,
}
});
TrakingEnabled = true;
}

export function trackEvent(name: string, params: any = {}) {
if (!TrakingEnabled) {
return;
}
appsFlyer.logEvent(name, params);
logEvent(name, params);
}


export async function trackFirstLaunch() {
const isFirstLaunch = !(await AsyncStorage.getItem('launched_before'));
if (isFirstLaunch) {
trackEvent('first_launch');
await AsyncStorage.setItem('launched_before', 'true');
}
}
56 changes: 51 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,52 @@
# yarn lockfile v1


"@amplitude/analytics-browser@^1.6.7":
version "1.6.7"
resolved "https://registry.yarnpkg.com/@amplitude/analytics-browser/-/analytics-browser-1.6.7.tgz#2d42db6e7c628ad7a5b0abe7323e045dda43ed67"
integrity sha512-XbiSH01hMpNrS7ymv5u5GB5Fwk1EO2EgWV3SZ0ck3ajKQZQg1/Q1p0TggrB79pSG047xYIdVQjoD0XzAtPdOug==
dependencies:
"@amplitude/analytics-client-common" "^0.5.1"
"@amplitude/analytics-core" "^0.11.1"
"@amplitude/analytics-types" "^0.14.0"
"@amplitude/ua-parser-js" "^0.7.31"
tslib "^2.4.1"

"@amplitude/analytics-client-common@^0.5.1":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@amplitude/analytics-client-common/-/analytics-client-common-0.5.1.tgz#8ecf6db65fc59c53b624ddeda02573452b5f9271"
integrity sha512-RmIRJ1JG7Kre1bNY6x+mSp/LjnWNZngwL50zI9DsLuYw5fOmHCzJHn1Ntte+rQDlQ7dTr3jMg1zeP6+76CMw2Q==
dependencies:
"@amplitude/analytics-connector" "^1.4.5"
"@amplitude/analytics-core" "^0.11.1"
"@amplitude/analytics-types" "^0.14.0"
tslib "^2.4.1"

"@amplitude/analytics-connector@^1.4.5":
version "1.4.6"
resolved "https://registry.yarnpkg.com/@amplitude/analytics-connector/-/analytics-connector-1.4.6.tgz#60a66eaf0bcdcd17db4f414ff340c69e63116a72"
integrity sha512-6jD2pOosRD4y8DT8StUCz7yTd5ZDkdOU9/AWnlWKM5qk90Mz7sdZrdZ9H7sA/L3yOJEpQOYZgQplQdWWUzyWug==
dependencies:
"@amplitude/ua-parser-js" "0.7.31"

"@amplitude/analytics-core@^0.11.1":
version "0.11.1"
resolved "https://registry.yarnpkg.com/@amplitude/analytics-core/-/analytics-core-0.11.1.tgz#faa801c1566dff9136091d6c9171b23ce4e18ef9"
integrity sha512-G+GgYFwxhD5DjYDKuC6jRcsO/kqJw4hLTlJ0x2mwppxVuMqQ44wRvdQ1hhzMRY02AGnmr0OR1xDtft8CabiIag==
dependencies:
"@amplitude/analytics-types" "^0.14.0"
tslib "^2.4.1"

"@amplitude/analytics-types@^0.14.0":
version "0.14.0"
resolved "https://registry.yarnpkg.com/@amplitude/analytics-types/-/analytics-types-0.14.0.tgz#06848235e52fe1d46a6e04bbc5d14090bdde15e3"
integrity sha512-O3E/KHyWCb4HAlYevqgCzqQdS+0LhbJV2IeVGox2eyCge+/lGwtGdJV2rJNWcoGf3p+BhnbqJBlhkPMs12iniA==

"@amplitude/[email protected]", "@amplitude/ua-parser-js@^0.7.31":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@amplitude/ua-parser-js/-/ua-parser-js-0.7.31.tgz#749bf7cb633cfcc7ff3c10805bad7c5f6fbdbc61"
integrity sha512-+z8UGRaj13Pt5NDzOnkTBy49HE2CX64jeL0ArB86HAtilpnfkPB7oqkigN7Lf2LxscMg4QhFD7mmCfedh3rqTg==

"@ampproject/remapping@^2.1.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d"
Expand Down Expand Up @@ -9368,11 +9414,6 @@ react-is@^16.13.0, react-is@^16.13.1, react-is@^16.7.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==

[email protected]:
version "6.5.21"
resolved "https://registry.yarnpkg.com/react-native-appsflyer/-/react-native-appsflyer-6.5.21.tgz#ab67214f05e29d8ff20a662d6ee2fd41d4ed9f2b"
integrity sha512-9xokVnTRYszhBdBLrwNH9poeQnH0p1wA9ojHACWkZgDjCUypnIIq+5DFqD5oTTE3jPS9alU0AyLGL7zpZZoT7Q==

react-native-camera@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/react-native-camera/-/react-native-camera-4.2.1.tgz#caf74081f055e89d7e9b0cd5108965d808c60e90"
Expand Down Expand Up @@ -11210,6 +11251,11 @@ tslib@^2.0.0, tslib@^2.0.1, tslib@^2.1.0, tslib@^2.4.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.0.tgz#7cecaa7f073ce680a05847aa77be941098f36dc3"
integrity sha512-d6xOpEDfsi2CZVlPQzGeux8XMwLT9hssAsaPYExaQMuYskwb+x1x7J371tWlbBdWHroy99KnVB6qIkUbs5X3UQ==

tslib@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.4.1.tgz#0d0bfbaac2880b91e22df0768e55be9753a5b17e"
integrity sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==

tsutils@^3.17.1:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
Expand Down

0 comments on commit 69fc360

Please sign in to comment.