Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix webview telemetry for new architecture apps #542

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/react-native-webview/src/NativeDdSdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.
*/

import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

/**
* We have to redefine the spec for the Native SDK here to be able to use the new architecture.
* We don't declare it in a spec file so we don't end up with a duplicate definition of the native module.
*/
interface PartialNativeDdSdkSpec extends TurboModule {
consumeWebviewEvent(message: string): Promise<void>;
telemetryError(message: string, stack: string, kind: string): Promise<void>;
}
export const NativeDdSdk = TurboModuleRegistry.get<PartialNativeDdSdkSpec>(
'DdSdk'
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import { NativeModules } from 'react-native';
import { NativeDdSdk } from '../NativeDdSdk';

export function formatAllowedHosts(allowedHosts?: string[]): string {
try {
return `'${JSON.stringify(allowedHosts)}'`;
} catch (e: any) {
if (NativeModules.DdSdk && NativeModules.DdSdk.telemetryError) {
NativeModules.DdSdk.telemetryError(
if (NativeDdSdk) {
NativeDdSdk.telemetryError(
getErrorMessage(e),
getErrorStackTrace(e),
'AllowedHostsError'
Expand Down
12 changes: 1 addition & 11 deletions packages/react-native-webview/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';
import { WebView as RNWebView } from 'react-native-webview';
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import React, { forwardRef, useCallback } from 'react';

import { NativeDdSdk } from './NativeDdSdk';
import {
DATADOG_MESSAGE_PREFIX,
getInjectedJavaScriptBeforeContentLoaded
Expand Down Expand Up @@ -50,12 +49,3 @@ const WebViewComponent = (props: Props, ref: React.Ref<RNWebView<Props>>) => {
export const WebView = forwardRef(WebViewComponent);

export default WebView;

/**
* We have to redefine the spec for the Native SDK here to be able to use the new architecture.
* We don't declare it in a separate file so we don't end up with a duplicate definition of the native module.
*/
interface PartialNativeDdSdkSpec extends TurboModule {
consumeWebviewEvent(message: string): Promise<void>;
}
const NativeDdSdk = TurboModuleRegistry.get<PartialNativeDdSdkSpec>('DdSdk');