From 05dd925e884590cdfcaaa89d60b346d0fa35e1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Berente?= <30603208+berenteb@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:38:20 +0100 Subject: [PATCH] feat: added analytics --- .env.example | 3 ++- app.config.ts | 2 ++ app/(tabs)/home/index.tsx | 2 +- app/(tabs)/map.tsx | 2 +- .../presentation/favorite-presentations.tsx | 2 +- app/(tabs)/presentation/index.tsx | 2 +- app/[...unmatched].tsx | 2 ++ components/base/screen.tsx | 8 ++++++- components/common/error-boundary.tsx | 2 ++ components/news/layouts/news-details-page.tsx | 5 ++--- .../layouts/presentation-details-page.tsx | 2 +- config/env.config.ts | 1 + services/analytics.service.ts | 21 +++++++++++++++++++ utils/analytics.utils.ts | 10 +++++++++ 14 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 services/analytics.service.ts create mode 100644 utils/analytics.utils.ts diff --git a/.env.example b/.env.example index 8c52f08..d794d10 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,2 @@ -EXPO_PUBLIC_API_BASE_URL= \ No newline at end of file +EXPO_PUBLIC_API_BASE_URL= +EXPO_PUBLIC_DISABLE_ANALYTICS=true \ No newline at end of file diff --git a/app.config.ts b/app.config.ts index c1a7693..1e1adf3 100644 --- a/app.config.ts +++ b/app.config.ts @@ -4,12 +4,14 @@ import * as env from 'env-var'; config(); const EXPO_PUBLIC_API_BASE_URL = env.get('EXPO_PUBLIC_API_BASE_URL').required().asString(); +const EXPO_PUBLIC_DISABLE_ANALYTICS = env.get('EXPO_PUBLIC_DISABLE_ANALYTICS').default('false').asBool(); export default ({ config }: ConfigContext) => { return { ...config, extra: { apiBaseUrl: EXPO_PUBLIC_API_BASE_URL, + disableAnalytics: EXPO_PUBLIC_DISABLE_ANALYTICS, ...config.extra, }, }; diff --git a/app/(tabs)/home/index.tsx b/app/(tabs)/home/index.tsx index d93d499..35b6033 100644 --- a/app/(tabs)/home/index.tsx +++ b/app/(tabs)/home/index.tsx @@ -18,7 +18,7 @@ export default function HomePage() { const conference = useConference(); const news = useNews(); return ( - +
Simonyi Konferencia
diff --git a/app/(tabs)/map.tsx b/app/(tabs)/map.tsx index ff97398..99c9a36 100644 --- a/app/(tabs)/map.tsx +++ b/app/(tabs)/map.tsx @@ -18,7 +18,7 @@ export default function MapPage() { }; return ( - +
Térkép
diff --git a/app/(tabs)/presentation/favorite-presentations.tsx b/app/(tabs)/presentation/favorite-presentations.tsx index f1f1f40..52e6555 100644 --- a/app/(tabs)/presentation/favorite-presentations.tsx +++ b/app/(tabs)/presentation/favorite-presentations.tsx @@ -11,7 +11,7 @@ import { useFavoritePresentationsList } from '../../../hooks/use-favorite-presen export default function FavoritePresentationsScreen() { const { data, isLoading, isError } = useFavoritePresentationsList(); return ( - +
Kedvenc előadásaim
diff --git a/app/(tabs)/presentation/index.tsx b/app/(tabs)/presentation/index.tsx index 14bda21..74a1708 100644 --- a/app/(tabs)/presentation/index.tsx +++ b/app/(tabs)/presentation/index.tsx @@ -13,7 +13,7 @@ import { useConference } from '../../../hooks/use-conference'; export default function PresentationListPage() { const { data, isError, isLoading } = useConference(); return ( - +
Programterv
diff --git a/app/[...unmatched].tsx b/app/[...unmatched].tsx index 2e399e6..f54227c 100644 --- a/app/[...unmatched].tsx +++ b/app/[...unmatched].tsx @@ -4,9 +4,11 @@ import { SafeAreaView } from 'react-native'; import { StyledButton } from '../components/common/styled-button'; import { Subtitle } from '../components/common/subtitle'; import { Title } from '../components/common/title'; +import { usePageView } from '../utils/analytics.utils'; export default function Unmatched() { const { canGoBack, back } = useRouter(); + usePageView('unmatched'); return ( Ismeretlen képernyőre jutottál diff --git a/components/base/screen.tsx b/components/base/screen.tsx index 0bbc92c..7ba6d9a 100644 --- a/components/base/screen.tsx +++ b/components/base/screen.tsx @@ -1,7 +1,13 @@ import { View, ViewProps } from 'react-native'; +import { usePageView } from '../../utils/analytics.utils'; import { cn } from '../../utils/common.utils'; -export function Screen({ className, ...props }: ViewProps) { +interface ScreenProps extends ViewProps { + analyticsScreenName?: string; +} + +export function Screen({ className, analyticsScreenName, ...props }: ScreenProps) { + usePageView(analyticsScreenName); return ; } diff --git a/components/common/error-boundary.tsx b/components/common/error-boundary.tsx index ccb641d..0fd4b27 100644 --- a/components/common/error-boundary.tsx +++ b/components/common/error-boundary.tsx @@ -2,12 +2,14 @@ import { ErrorBoundaryProps } from 'expo-router'; import { ScrollView, View } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; +import { usePageView } from '../../utils/analytics.utils'; import { StyledButton } from './styled-button'; import { Subtitle } from './subtitle'; import { Title } from './title'; export function ErrorBoundary(props: ErrorBoundaryProps) { const { top, bottom, left, right } = useSafeAreaInsets(); + usePageView('error'); return ( ; return ( - +
{isLoading && } - {data.title && {data.title}} + {data?.title && {data.title}}
{error && ( diff --git a/components/schedule/layouts/presentation-details-page.tsx b/components/schedule/layouts/presentation-details-page.tsx index 4a12881..6b214a5 100644 --- a/components/schedule/layouts/presentation-details-page.tsx +++ b/components/schedule/layouts/presentation-details-page.tsx @@ -19,7 +19,7 @@ export function PresentationDetailsPage({ slug }: ScheduleDetailsPageProps) { const startTime = ConferenceService.getFormattedTimestamp(data?.startTime ?? ''); const endTime = ConferenceService.getFormattedTimestamp(data?.endTime ?? ''); return ( - +
: undefined}> {isLoading && } {data && ( diff --git a/config/env.config.ts b/config/env.config.ts index 1801f34..56da28a 100644 --- a/config/env.config.ts +++ b/config/env.config.ts @@ -1,3 +1,4 @@ import Constants from 'expo-constants'; export const API_BASE_URL = Constants.expoConfig?.extra?.apiBaseUrl; +export const DISABLE_ANALYTICS = Constants.expoConfig?.extra?.disableAnalytics; diff --git a/services/analytics.service.ts b/services/analytics.service.ts new file mode 100644 index 0000000..5bd57ac --- /dev/null +++ b/services/analytics.service.ts @@ -0,0 +1,21 @@ +import axios from 'axios'; + +import { DISABLE_ANALYTICS } from '../config/env.config'; + +export class AnalyticsService { + static sendPageView(location: string) { + const eventBody = { + name: 'pageview', + domain: 'konferenciapp.kir-dev.hu', + url: 'com.kir-dev.konferenciapp://localhost/' + location, + }; + + if (DISABLE_ANALYTICS) { + console.debug('Analytics event', eventBody); + } else { + axios.post('https://visit.kir-dev.hu/api/event', eventBody).catch((error) => { + console.error('Error during analytics event: ', error); + }); + } + } +} diff --git a/utils/analytics.utils.ts b/utils/analytics.utils.ts new file mode 100644 index 0000000..435843c --- /dev/null +++ b/utils/analytics.utils.ts @@ -0,0 +1,10 @@ +import { useEffect } from 'react'; + +import { AnalyticsService } from '../services/analytics.service'; + +export function usePageView(location?: string) { + useEffect(() => { + if (!location) return; + AnalyticsService.sendPageView(location); + }, []); +}