From 61507dc3a9a23aa88bc1f626747dc4a5b04b8cd1 Mon Sep 17 00:00:00 2001 From: auto200 <49625375+auto200@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:08:26 +0100 Subject: [PATCH] feat: content tracking example --- .../ContentTrackingMethods.tsx | 44 ++++++++++++ .../ContentTrackingForm.tsx | 57 +++++++++++++++ .../components/ContentTrackingForm/Login.tsx | 22 ++++++ .../ContentTrackingForm/Register.tsx | 31 ++++++++ example/src/pages/ContentTrackingPage.tsx | 71 +++++++++++++++++++ example/src/routes.tsx | 6 ++ src/constants/track-event.constant.ts | 1 + .../contentTracking.service.ts | 4 ++ 8 files changed, 236 insertions(+) create mode 100644 example/src/components/ContentTracking/ContentTrackingMethods.tsx create mode 100644 example/src/components/ContentTrackingForm/ContentTrackingForm.tsx create mode 100644 example/src/components/ContentTrackingForm/Login.tsx create mode 100644 example/src/components/ContentTrackingForm/Register.tsx create mode 100644 example/src/pages/ContentTrackingPage.tsx diff --git a/example/src/components/ContentTracking/ContentTrackingMethods.tsx b/example/src/components/ContentTracking/ContentTrackingMethods.tsx new file mode 100644 index 0000000..fd711b1 --- /dev/null +++ b/example/src/components/ContentTracking/ContentTrackingMethods.tsx @@ -0,0 +1,44 @@ +import { Box, Button, Paper } from '@mui/material' +import { ElementRef, useEffect, useRef } from 'react' +import { ContentTracking } from '@piwikpro/react-piwik-pro' + +export function ContentTrackingMethods() { + const ref = useRef>(null) + + useEffect(() => { + if (!ref.current) return + + ContentTracking.trackContentImpressionsWithinNode(ref.current) + ContentTracking.trackContentInteractionNode(ref.current, 'click') + }, []) + + return ( + <> + + + Track content within a specific dom node + + + + + + + + ) +} diff --git a/example/src/components/ContentTrackingForm/ContentTrackingForm.tsx b/example/src/components/ContentTrackingForm/ContentTrackingForm.tsx new file mode 100644 index 0000000..2d02140 --- /dev/null +++ b/example/src/components/ContentTrackingForm/ContentTrackingForm.tsx @@ -0,0 +1,57 @@ +import Box from '@mui/material/Box' +import Tab from '@mui/material/Tab' +import Tabs from '@mui/material/Tabs' +import { useState } from 'react' +import { Login } from './Login' +import { Register } from './Register' + +interface TabPanelProps { + children?: React.ReactNode + index: number + value: number +} + +function CustomTabPanel(props: TabPanelProps) { + const { children, value, index, ...other } = props + + return ( + + ) +} + +export function ContentTrackingForm() { + const [value, setValue] = useState(0) + + const handleChange = (_event: React.SyntheticEvent, newValue: number) => { + setValue(newValue) + } + + return ( + + + + + + + + + + + + + + + ) +} diff --git a/example/src/components/ContentTrackingForm/Login.tsx b/example/src/components/ContentTrackingForm/Login.tsx new file mode 100644 index 0000000..383a8c7 --- /dev/null +++ b/example/src/components/ContentTrackingForm/Login.tsx @@ -0,0 +1,22 @@ +import { Button, Stack, TextField } from '@mui/material' +import { useEffect } from 'react' +import { ContentTracking } from '@piwikpro/react-piwik-pro' + +export function Login() { + // NOTE: candidate for a custom hook useTrackAllContentImpressions() + useEffect(() => { + ContentTracking.trackAllContentImpressions() + + return () => ContentTracking.clearTrackedContentImpressions() + }, []) + + return ( + + + + + + ) +} diff --git a/example/src/components/ContentTrackingForm/Register.tsx b/example/src/components/ContentTrackingForm/Register.tsx new file mode 100644 index 0000000..afd3b0c --- /dev/null +++ b/example/src/components/ContentTrackingForm/Register.tsx @@ -0,0 +1,31 @@ +import { Button, Stack, TextField } from '@mui/material' +import { ContentTracking } from '@piwikpro/react-piwik-pro' +import { useEffect } from 'react' + +export function Register() { + useEffect(() => { + ContentTracking.trackAllContentImpressions() + + return () => ContentTracking.clearTrackedContentImpressions() + }, []) + + return ( + + + + + + + + ) +} diff --git a/example/src/pages/ContentTrackingPage.tsx b/example/src/pages/ContentTrackingPage.tsx new file mode 100644 index 0000000..46e764d --- /dev/null +++ b/example/src/pages/ContentTrackingPage.tsx @@ -0,0 +1,71 @@ +import { Box, CircularProgress } from '@mui/material' +import Paper from '@mui/material/Paper' +import Typography from '@mui/material/Typography' +import { FunctionComponent, useEffect, useState } from 'react' +import { ContentTrackingMethods } from '../components/ContentTracking/ContentTrackingMethods' +import { ContentTrackingForm } from '../components/ContentTrackingForm/ContentTrackingForm' + +const ContentTrackingPage: FunctionComponent = () => { + const [isLoading, setIsLoading] = useState(true) + + useEffect(() => { + document.title = 'Content Tracking' + }, []) + + useEffect(() => { + const timeout = setTimeout(() => { + setIsLoading(false) + }, 3000) + + return () => { + clearTimeout(timeout) + } + }, []) + + return ( + + + Content Tracking Example + + + + + + This element will be tracked automatically if it's present in DOM + before tracking script is loaded and `Interactions with popups and + content` setting is turned on in the administration panel + + + + + + For content that will appear later in the DOM, like modals or + components that are waiting for data to be loaded, use{' '} + ContentTracking module + + + + {isLoading ? : } + + + + + + + + + ) +} + +export default ContentTrackingPage diff --git a/example/src/routes.tsx b/example/src/routes.tsx index 51d09e1..8f0a169 100644 --- a/example/src/routes.tsx +++ b/example/src/routes.tsx @@ -2,6 +2,7 @@ import HomePage from './pages/HomePage.tsx' import ECommercePage from './pages/ECommercePage.tsx' import CustomEventPage from './pages/CustomEventPage.tsx' import GoalConversionsPage from './pages/GoalConversionsPage.tsx' +import ContentTrackingPage from './pages/ContentTrackingPage.tsx' export const routes = [ { @@ -23,5 +24,10 @@ export const routes = [ path: '/goal-conversions', name: 'Goal Conversions', element: + }, + { + path: '/content-tracking', + name: 'Content Tracking', + element: } ] diff --git a/src/constants/track-event.constant.ts b/src/constants/track-event.constant.ts index c207f5d..0f5bdb6 100644 --- a/src/constants/track-event.constant.ts +++ b/src/constants/track-event.constant.ts @@ -27,6 +27,7 @@ export enum TRACK_EVENT { LOG_ALL_CONTENT_BLOCKS_ON_PAGE = 'logAllContentBlocksOnPage', CONTENT_INTERACTION_NODE = 'trackContentInteractionNode', CONTENT_INTERACTION = 'trackContentInteraction', + CLEAR_TRACKED_CONTENT_IMPRESSIONS = 'clearTrackedContentImpressions', LINK = 'trackLink', ENABLE_LINK_TRACKING = 'enableLinkTracking', SET_IGNORE_CLASSES = 'setIgnoreClasses', diff --git a/src/services/content-tracking/contentTracking.service.ts b/src/services/content-tracking/contentTracking.service.ts index 502c1e4..f77b82f 100644 --- a/src/services/content-tracking/contentTracking.service.ts +++ b/src/services/content-tracking/contentTracking.service.ts @@ -52,3 +52,7 @@ export function trackContentInteraction(contentInteraction: string, contentName: contentTarget ]); } + +export function clearTrackedContentImpressions(){ + PaqService.push([TRACK_EVENT.CLEAR_TRACKED_CONTENT_IMPRESSIONS]) +} \ No newline at end of file