Skip to content

Commit

Permalink
Improve launch event
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmell committed Aug 22, 2023
1 parent d88f9f1 commit 2b5b326
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function DisableNotificationButton({
// FCM Token Disabled.
fcmPushNotificationStorage.remove()
setIsRegistered(false)
sendEvent('wp_notifs_disabled', undefined, { wpNotifsEnabled: false })
sendEvent('wp_notifs_disabled', undefined, { webNotifsEnabled: false })
},
})

Expand Down Expand Up @@ -119,7 +119,7 @@ function EnableNotificationButton({
if (fcmToken) {
fcmPushNotificationStorage.set(fcmToken)
setIsRegistered(true)
sendEvent('wp_notifs_allowed', undefined, { wpNotifsEnabled: true })
sendEvent('wp_notifs_allowed', undefined, { webNotifsEnabled: true })
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion src/stores/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type UserProperties = {
pwaInstalled?: boolean
evmLinked?: boolean
tgNotifsConnected?: boolean
wpNotifsEnabled?: boolean
webNotifsEnabled?: boolean
ownedChat?: boolean
hasJoinedChats?: boolean
}
Expand Down
63 changes: 42 additions & 21 deletions src/stores/my-account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getLinkedTelegramAccountsQuery } from '@/services/api/notifications/query'
import { queryClient } from '@/services/provider'
import { getAccountsData } from '@/services/subsocial/evmAddresses'
import { getOwnedPostIdsQuery } from '@/services/subsocial/posts'
import { useParentData } from '@/stores/parent'
import {
decodeSecretKey,
Expand Down Expand Up @@ -57,6 +58,43 @@ export const followedIdsStorage = new LocalStorage(
)
const accountStorage = new LocalStorage(() => ACCOUNT_STORAGE_KEY)

const sendLaunchEvent = async (address?: string | false) => {
let userProperties = {
tgNotifsConnected: false,
evmLinked: false,
webNotifsEnabled: false,
ownedChat: false,
}

const sendEvent = useAnalytics.getState().sendEvent

if (!address) {
sendEvent('launch_app', undefined, userProperties)
} else {
const linkedTgAccData = await getLinkedTelegramAccountsQuery.fetchQuery(
queryClient,
{
address,
}
)
userProperties.tgNotifsConnected = (linkedTgAccData?.length || 0) > 0

const [evmLinkedAddress] = await getAccountsData([address])

userProperties.evmLinked = !!evmLinkedAddress

const ownedPostIds = await getOwnedPostIdsQuery.fetchQuery(
queryClient,
address
)
userProperties.ownedChat = (ownedPostIds?.length || 0) > 0

userProperties.webNotifsEnabled = isWebNotificationsEnabled()

sendEvent('launch_app', undefined, userProperties)
}
}

export const useMyAccount = create<State & Actions>()((set, get) => ({
...initialState,
login: async (secretKey, isInitialization) => {
Expand Down Expand Up @@ -161,12 +199,6 @@ export const useMyAccount = create<State & Actions>()((set, get) => ({

const encodedSecretKey = accountStorage.get()

let userProperties = {
tgNotifsConnected: false,
evmLinked: false,
webNotifsEnabled: isWebNotificationsEnabled(),
}

if (encodedSecretKey) {
const storageAddress = accountAddressStorage.get()
set({ address: storageAddress || undefined })
Expand All @@ -178,23 +210,12 @@ export const useMyAccount = create<State & Actions>()((set, get) => ({
accountStorage.remove()
accountAddressStorage.remove()
set({ address: null })
} else {
// TODO: check how we can do it more elegant
const linkedTgAccData = await getLinkedTelegramAccountsQuery.fetchQuery(
queryClient,
{
address,
}
)
userProperties.tgNotifsConnected = (linkedTgAccData?.length || 0) > 0

const [evmLinkedAddress] = await getAccountsData([address])

userProperties.evmLinked = !!evmLinkedAddress
}
}

useAnalytics.getState().sendEvent('launch_app', undefined, userProperties)
sendLaunchEvent(address)
} else {
sendLaunchEvent()
}

set({ isInitialized: true })
},
Expand Down

0 comments on commit 2b5b326

Please sign in to comment.