@datadog/mobile-react-native-navigation
Version:
A client-side React Native module to interact with Datadog
116 lines (88 loc) • 4.34 kB
JavaScript
;
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");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/**
* Provides RUM integration for the [React Native Navigation](https://wix.github.io/react-native-navigation) API.
*/
class DdRumReactNativeNavigationTracking {
/**
* Starts tracking the Navigation and sends a RUM View event every time a root View component appear/disappear.
*/
static startTracking(viewNamePredicate = function (_event, trackedName) {
return trackedName;
}) {
// extra safety to avoid wrapping more than 1 time this function
if (DdRumReactNativeNavigationTracking.isTracking) {
return;
}
DdRumReactNativeNavigationTracking.eventSubscription = _reactNativeNavigation.Navigation.events().registerComponentDidAppearListener(event => {
const predicate = DdRumReactNativeNavigationTracking.viewNamePredicate;
const screenName = predicate(event, event.componentName);
if (screenName !== null) {
_mobileReactNative.DdRum.startView(event.componentId, screenName);
DdRumReactNativeNavigationTracking.lastView = {
key: event.componentId,
name: screenName
};
}
});
DdRumReactNativeNavigationTracking.isTracking = true;
DdRumReactNativeNavigationTracking.viewNamePredicate = viewNamePredicate;
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 = function (_event, trackedName) {
return trackedName;
};
}
}
exports.DdRumReactNativeNavigationTracking = DdRumReactNativeNavigationTracking;
_defineProperty(DdRumReactNativeNavigationTracking, "isTracking", false);
_defineProperty(DdRumReactNativeNavigationTracking, "eventSubscription", undefined);
_defineProperty(DdRumReactNativeNavigationTracking, "appStateSubscription", void 0);
_defineProperty(DdRumReactNativeNavigationTracking, "viewNamePredicate", void 0);
_defineProperty(DdRumReactNativeNavigationTracking, "lastView", 'tracking_not_started');
_defineProperty(DdRumReactNativeNavigationTracking, "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);
}
});
//# sourceMappingURL=DdRumReactNativeNavigationTracking.js.map