|
| 1 | +import { |
| 2 | + buildSelfDescribingEvent, |
| 3 | + CorePluginConfiguration, |
| 4 | + SelfDescribingJson, |
| 5 | + TrackerCore, |
| 6 | +} from '@snowplow/tracker-core'; |
| 7 | +import { AppLifecycleConfiguration, EventContext } from '../../types'; |
| 8 | +import { BACKGROUND_EVENT_SCHEMA, FOREGROUND_EVENT_SCHEMA, LIFECYCLE_CONTEXT_SCHEMA } from '../../constants'; |
| 9 | +import { AppState } from 'react-native'; |
| 10 | + |
| 11 | +export interface AppLifecyclePlugin extends CorePluginConfiguration { |
| 12 | + getIsInBackground: () => boolean | undefined; |
| 13 | + getBackgroundIndex: () => number | undefined; |
| 14 | + getForegroundIndex: () => number | undefined; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Tracks foreground and background events automatically when the app state changes. |
| 19 | + * Also adds a lifecycle context to all events with information about the app visibility. |
| 20 | + */ |
| 21 | +export async function newAppLifecyclePlugin( |
| 22 | + { lifecycleAutotracking = true }: AppLifecycleConfiguration, |
| 23 | + core: TrackerCore |
| 24 | +): Promise<AppLifecyclePlugin> { |
| 25 | + let isInForeground = AppState.currentState !== 'background'; |
| 26 | + let foregroundIndex = isInForeground ? 1 : 0; |
| 27 | + let backgroundIndex = isInForeground ? 0 : 1; |
| 28 | + let subscription: ReturnType<typeof AppState.addEventListener> | undefined; |
| 29 | + |
| 30 | + if (lifecycleAutotracking) { |
| 31 | + // Subscribe to app state changes and track foreground/background events |
| 32 | + subscription = AppState.addEventListener('change', async (nextAppState) => { |
| 33 | + if (nextAppState === 'active' && !isInForeground) { |
| 34 | + trackForegroundEvent(); |
| 35 | + } |
| 36 | + if (nextAppState === 'background' && isInForeground) { |
| 37 | + trackBackgroundEvent(); |
| 38 | + } |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + const contexts = () => { |
| 43 | + let entities: SelfDescribingJson[] = []; |
| 44 | + |
| 45 | + if (lifecycleAutotracking) { |
| 46 | + // Add lifecycle context to all events |
| 47 | + entities.push({ |
| 48 | + schema: LIFECYCLE_CONTEXT_SCHEMA, |
| 49 | + data: { |
| 50 | + isVisible: isInForeground, |
| 51 | + index: isInForeground ? foregroundIndex : backgroundIndex, |
| 52 | + }, |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + return entities; |
| 57 | + }; |
| 58 | + |
| 59 | + const deactivatePlugin = () => { |
| 60 | + if (subscription) { |
| 61 | + subscription.remove(); |
| 62 | + subscription = undefined; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const trackForegroundEvent = (contexts?: EventContext[]) => { |
| 67 | + if (!isInForeground) { |
| 68 | + isInForeground = true; |
| 69 | + foregroundIndex += 1; |
| 70 | + } |
| 71 | + core.track( |
| 72 | + buildSelfDescribingEvent({ event: { schema: FOREGROUND_EVENT_SCHEMA, data: { foregroundIndex } } }), |
| 73 | + contexts |
| 74 | + ); |
| 75 | + }; |
| 76 | + |
| 77 | + const trackBackgroundEvent = (contexts?: EventContext[]) => { |
| 78 | + if (isInForeground) { |
| 79 | + isInForeground = false; |
| 80 | + backgroundIndex += 1; |
| 81 | + } |
| 82 | + core.track( |
| 83 | + buildSelfDescribingEvent({ event: { schema: BACKGROUND_EVENT_SCHEMA, data: { backgroundIndex } } }), |
| 84 | + contexts |
| 85 | + ); |
| 86 | + }; |
| 87 | + |
| 88 | + return { |
| 89 | + getIsInBackground: () => (lifecycleAutotracking ? !isInForeground : undefined), |
| 90 | + getBackgroundIndex: () => (lifecycleAutotracking ? backgroundIndex : undefined), |
| 91 | + getForegroundIndex: () => (lifecycleAutotracking ? foregroundIndex : undefined), |
| 92 | + plugin: { |
| 93 | + contexts, |
| 94 | + deactivatePlugin, |
| 95 | + }, |
| 96 | + }; |
| 97 | +} |
0 commit comments