Skip to content

Commit

Permalink
TW-1127 Add AnalyticsAndAdsEnabled analytics event
Browse files Browse the repository at this point in the history
  • Loading branch information
keshan3262 committed Oct 27, 2023
1 parent e5bbf15 commit 01ec465
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 23 deletions.
25 changes: 18 additions & 7 deletions src/app/hooks/use-user-analytics-and-ads-settings.hook.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import { useEffect } from 'react';
import { useEffect, useRef } from 'react';

import { useAnalytics } from 'lib/analytics';
import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants';
import { AnalyticsEventCategory } from 'lib/temple/analytics-types';
import { useAccountPkh } from 'lib/temple/front';
import { usePassiveStorage } from 'lib/temple/front/storage';

import { usePassiveStorage } from '../../lib/temple/front/storage';
import { useShouldShowPartnersPromoSelector } from '../store/partners-promotion/selectors';
import { useAnalyticsEnabledSelector } from '../store/settings/selectors';

const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED';

export const useUserAnalyticsAndAdsSettings = () => {
const { trackEvent } = useAnalytics();
const isAnalyticsEnabled = useAnalyticsEnabledSelector();
const isAdsEnabled = useShouldShowPartnersPromoSelector();

const [, setIsWebsitesAnalyticsEnabled] = usePassiveStorage(WEBSITES_ANALYTICS_ENABLED);
const prevWebsiteAnalyticsEnabledRef = useRef(isAnalyticsEnabled && isAdsEnabled);
const accountPkh = useAccountPkh();

useEffect(() => {
const isAnalyticsAndAdsEnabled = isAnalyticsEnabled && isAdsEnabled;
const shouldEnableAnalyticsAndAds = isAnalyticsEnabled && isAdsEnabled;

setIsWebsitesAnalyticsEnabled(shouldEnableAnalyticsAndAds);

setIsWebsitesAnalyticsEnabled(isAnalyticsAndAdsEnabled);
}, [isAnalyticsEnabled, isAdsEnabled, setIsWebsitesAnalyticsEnabled]);
if (shouldEnableAnalyticsAndAds && !prevWebsiteAnalyticsEnabledRef.current) {
trackEvent('AnalyticsAndAdsEnabled', AnalyticsEventCategory.General, { accountPkh });
}
prevWebsiteAnalyticsEnabledRef.current = shouldEnableAnalyticsAndAds;
}, [isAnalyticsEnabled, isAdsEnabled, setIsWebsitesAnalyticsEnabled, trackEvent, accountPkh]);
};
2 changes: 2 additions & 0 deletions src/app/pages/Home/OtherComponents/Tokens/AcceptAdsBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { FC, useCallback, useMemo } from 'react';
import { useDispatch } from 'react-redux';

import { Banner, BannerButtonProps } from 'app/atoms/Banner';
import { useUserAnalyticsAndAdsSettings } from 'app/hooks/use-user-analytics-and-ads-settings.hook';
import { togglePartnersPromotionAction } from 'app/store/partners-promotion/actions';
import { setAdsBannerVisibilityAction } from 'app/store/settings/actions';
import { T } from 'lib/i18n';
Expand All @@ -13,6 +14,7 @@ import { AssetsSelectors } from '../Assets.selectors';
export const AcceptAdsBanner: FC = () => {
const dispatch = useDispatch();

useUserAnalyticsAndAdsSettings();
const onEnableButtonClick = useCallback(() => {
dispatch(togglePartnersPromotionAction(true));
dispatch(setAdsBannerVisibilityAction(false));
Expand Down
43 changes: 30 additions & 13 deletions src/app/pages/NewWallet/setWalletPassword/SetWalletPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import {
setOnRampPossibilityAction
} from 'app/store/settings/actions';
import { AnalyticsEventCategory, TestIDProps, useAnalytics } from 'lib/analytics';
import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants';
import { T, t } from 'lib/i18n';
import { putToStorage } from 'lib/storage';
import { useTempleClient } from 'lib/temple/front';
import PasswordStrengthIndicator, { PasswordValidation } from 'lib/ui/PasswordStrengthIndicator';
import { navigate } from 'lib/woozie';
Expand Down Expand Up @@ -58,16 +60,22 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({

const dispatch = useDispatch();

const setAnalyticsEnabled = (analyticsEnabled: boolean) => dispatch(setIsAnalyticsEnabledAction(analyticsEnabled));
const setAdsViewEnabled = (adsViewEnabled: boolean) => {
if (adsViewEnabled) {
dispatch(setAdsBannerVisibilityAction(false));
dispatch(togglePartnersPromotionAction(true));
} else {
dispatch(setAdsBannerVisibilityAction(true));
dispatch(togglePartnersPromotionAction(false));
}
};
const setAnalyticsEnabled = useCallback(
(analyticsEnabled: boolean) => dispatch(setIsAnalyticsEnabledAction(analyticsEnabled)),
[dispatch]
);
const setAdsViewEnabled = useCallback(
(adsViewEnabled: boolean) => {
if (adsViewEnabled) {
dispatch(setAdsBannerVisibilityAction(false));
dispatch(togglePartnersPromotionAction(true));
} else {
dispatch(setAdsBannerVisibilityAction(true));
dispatch(togglePartnersPromotionAction(false));
}
},
[dispatch]
);

const { setOnboardingCompleted } = useOnboardingProgress();

Expand Down Expand Up @@ -128,17 +136,24 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({
: data.password
: data.password;
try {
const shouldEnableAnalytics = Boolean(data.analytics);
setAdsViewEnabled(data.viewAds);
setAnalyticsEnabled(!!data.analytics);
setAnalyticsEnabled(shouldEnableAnalytics);
const shouldEnableWebsiteAnalytics = data.viewAds && shouldEnableAnalytics;
await putToStorage(WEBSITES_ANALYTICS_ENABLED, shouldEnableWebsiteAnalytics);

setOnboardingCompleted(data.skipOnboarding!);

await registerWallet(password!, formatMnemonic(seedPhrase));
const accountPkh = await registerWallet(password!, formatMnemonic(seedPhrase));
trackEvent(
data.skipOnboarding ? 'OnboardingSkipped' : 'OnboardingNotSkipped',
AnalyticsEventCategory.General,
undefined,
data.analytics
);
if (shouldEnableWebsiteAnalytics) {
trackEvent('AnalyticsAndAdsEnabled', AnalyticsEventCategory.General, { accountPkh }, data.analytics);
}
navigate('/loading');
!ownMnemonic && dispatch(setOnRampPossibilityAction(true));
dispatch(shouldShowNewsletterModalAction(true));
Expand All @@ -154,11 +169,13 @@ export const SetWalletPassword: FC<SetWalletPasswordProps> = ({
isKeystorePasswordWeak,
ownMnemonic,
keystorePassword,
setAdsViewEnabled,
setAnalyticsEnabled,
setOnboardingCompleted,
registerWallet,
seedPhrase,
trackEvent
trackEvent,
dispatch
]
);

Expand Down
2 changes: 2 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export enum ContentScriptType {
ExternalLinksActivity = 'ExternalLinksActivity'
}

export const WEBSITES_ANALYTICS_ENABLED = 'WEBSITES_ANALYTICS_ENABLED';

export const ACCOUNT_PKH_STORAGE_KEY = 'account_publickeyhash';
4 changes: 3 additions & 1 deletion src/lib/temple/back/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ export async function isDAppEnabled() {

export function registerNewWallet(password: string, mnemonic?: string) {
return withInited(async () => {
await Vault.spawn(password, mnemonic);
const accountPkh = await Vault.spawn(password, mnemonic);
await unlock(password);

return accountPkh;
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/temple/back/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const processRequest = async (req: TempleRequest, port: Runtime.Port): Promise<T
};

case TempleMessageType.NewWalletRequest:
await Actions.registerNewWallet(req.password, req.mnemonic);
return { type: TempleMessageType.NewWalletResponse };
const accountPkh = await Actions.registerNewWallet(req.password, req.mnemonic);
return { type: TempleMessageType.NewWalletResponse, accountPkh };

case TempleMessageType.UnlockRequest:
await Actions.unlock(req.password);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/temple/back/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export class Vault {
return SessionStore.removePassHash();
}

/**
* Creates a new wallet and saves it securely.
* @param password Password for encryption
* @param mnemonic Seed phrase
* @returns Initial account address
*/
static async spawn(password: string, mnemonic?: string) {
return withError('Failed to create wallet', async () => {
if (!mnemonic) {
Expand Down Expand Up @@ -121,6 +127,8 @@ export class Vault {
passKey
);
await savePlain(migrationLevelStrgKey, MIGRATIONS.length);

return accPublicKeyHash;
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib/temple/front/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export const [TempleClientProvider, useTempleClient] = constate(() => {
});
assertResponse(res.type === TempleMessageType.NewWalletResponse);
clearLocalStorage(['onboarding', 'analytics']);

return res.accountPkh;
}, []);

const unlock = useCallback(async (password: string) => {
Expand Down
1 change: 1 addition & 0 deletions src/lib/temple/front/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
useAllAccounts,
useSetAccountPkh,
useAccount,
useAccountPkh,
useSettings,
useTezos,
useChainId,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/temple/front/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const [
useAllAccounts,
useSetAccountPkh,
useAccount,
useAccountPkh,
useSettings,
useTezos
] = constate(
Expand All @@ -44,6 +45,7 @@ export const [
v => v.allAccounts,
v => v.setAccountPkh,
v => v.account,
v => v.accountPkh,
v => v.settings,
v => v.tezos
);
Expand Down
1 change: 1 addition & 0 deletions src/lib/temple/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ interface TempleNewWalletRequest extends TempleMessageBase {

interface TempleNewWalletResponse extends TempleMessageBase {
type: TempleMessageType.NewWalletResponse;
accountPkh: string;
}

interface TempleUnlockRequest extends TempleMessageBase {
Expand Down

0 comments on commit 01ec465

Please sign in to comment.