UNPKG

@datadog/mobile-react-native-navigation

Version:

A client-side React Native module to interact with Datadog

130 lines (123 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DdRumReactNativeNavigationTracking = void 0; var _mobileReactNative = require("@datadog/mobile-react-native"); var _reactNativeNavigation = require("react-native-navigation"); var _reactNative = require("react-native"); /* * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. * This product includes software developed at Datadog (https://www.datadoghq.com/). * Copyright 2016-Present Datadog, Inc. */ function defaultViewNamePredicate(_event, trackedName) { return trackedName; } function defaultParamsPredicate(_event) { return undefined; } function defaultViewTrackingPredicate(_event) { return true; } // AppStateStatus can have values: // 'active' - The app is running in the foreground // 'background' - The app is running in the background. The user is either in another app or on the home screen // 'inactive' [iOS] - This is a transition state that currently never happens for typical React Native apps. // 'unknown' [iOS] - Initial value until the current app state is determined // 'extension' [iOS] - The app is running as an app extension /** * Provides RUM integration for the [React Native Navigation](https://wix.github.io/react-native-navigation) API. */ class DdRumReactNativeNavigationTracking { static isTracking = false; static eventSubscription = undefined; static lastView = 'tracking_not_started'; /** * Starts tracking the Navigation and sends a RUM View event every time a root View component appear/disappear. */ static startTracking(trackingOptions) { // extra safety to avoid wrapping more than 1 time this function if (DdRumReactNativeNavigationTracking.isTracking) { return; } const { viewNamePredicate = defaultViewNamePredicate, viewTrackingPredicate = defaultViewTrackingPredicate, paramsTrackingPredicate = defaultParamsPredicate } = trackingOptions ?? {}; DdRumReactNativeNavigationTracking.eventSubscription = _reactNativeNavigation.Navigation.events().registerComponentDidAppearListener(event => { const predicate = DdRumReactNativeNavigationTracking.viewNamePredicate; const screenName = predicate(event, event.componentName); const shouldTrack = DdRumReactNativeNavigationTracking.viewTrackingPredicate(event); if (screenName !== null && shouldTrack) { const passProps = DdRumReactNativeNavigationTracking.paramsTrackingPredicate(event); if (passProps) { _mobileReactNative.DdRum.startView(event.componentId, screenName, { passProps }); } else { _mobileReactNative.DdRum.startView(event.componentId, screenName); } DdRumReactNativeNavigationTracking.lastView = { key: event.componentId, name: screenName }; } }); DdRumReactNativeNavigationTracking.isTracking = true; DdRumReactNativeNavigationTracking.viewNamePredicate = viewNamePredicate; DdRumReactNativeNavigationTracking.viewTrackingPredicate = viewTrackingPredicate; DdRumReactNativeNavigationTracking.paramsTrackingPredicate = paramsTrackingPredicate; this.appStateSubscription = _reactNative.AppState.addEventListener('change', DdRumReactNativeNavigationTracking.appStateListener); } /** * Stops tracking Navigation. */ static stopTracking() { if (!DdRumReactNativeNavigationTracking.isTracking) { return; } if (DdRumReactNativeNavigationTracking.eventSubscription) { DdRumReactNativeNavigationTracking.eventSubscription.remove(); } // For versions of React Native below 0.65, addEventListener does not return a subscription. // We have to call AppState.removeEventListener instead. if (this.appStateSubscription) { this.appStateSubscription.remove(); } else { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore _reactNative.AppState.removeEventListener('change', DdRumReactNativeNavigationTracking.appStateListener); } DdRumReactNativeNavigationTracking.lastView = undefined; DdRumReactNativeNavigationTracking.isTracking = false; DdRumReactNativeNavigationTracking.viewNamePredicate = defaultViewNamePredicate; DdRumReactNativeNavigationTracking.viewTrackingPredicate = defaultViewTrackingPredicate; DdRumReactNativeNavigationTracking.paramsTrackingPredicate = defaultParamsPredicate; // eslint-disable-next-line func-names DdRumReactNativeNavigationTracking.viewNamePredicate = function (_event, trackedName) { return trackedName; }; } static appStateListener = appStateStatus => { const lastView = DdRumReactNativeNavigationTracking.lastView; if (lastView === undefined) { _mobileReactNative.InternalLog.log(`We could not determine the route when changing the application state to: ${appStateStatus}. No RUM View event will be sent in this case.`, _mobileReactNative.SdkVerbosity.ERROR); return; } if (lastView === 'tracking_not_started') { // Do nothing as no view has been tracked already return; } if (appStateStatus === 'background') { _mobileReactNative.DdRum.stopView(lastView.key); } else if (appStateStatus === 'active' || appStateStatus === undefined) { // case when app goes into foreground, // in that case navigation listener won't be called _mobileReactNative.DdRum.startView(lastView.key, lastView.name); } }; } exports.DdRumReactNativeNavigationTracking = DdRumReactNativeNavigationTracking; //# sourceMappingURL=DdRumReactNativeNavigationTracking.js.map