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

feat: add app tracking transparency to webviews #33

Merged
merged 2 commits into from
Jul 30, 2024
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
7 changes: 7 additions & 0 deletions app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export default () => {
sounds: [],
},
],
[
'expo-tracking-transparency',
{
userTrackingPermission:
'This identifier will be used for providing services, personalization of services, statistical purposes and for legal obligations.',
},
],
],
experiments: {
typedRoutes: true,
Expand Down
10 changes: 10 additions & 0 deletions components/WhitelistedWebview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useNavigation } from '@react-navigation/native';
import { useTrackingPermissions } from 'expo-tracking-transparency';
import { useEffect, useRef } from 'react';
import { BackHandler, Linking, Platform } from 'react-native';
import WebView, { WebViewProps } from 'react-native-webview';
Expand All @@ -20,6 +21,7 @@ const injectedJavascript = `(function() {
})();`;

function WhitelistedWebview({ whitelistedUrls, ...props }: WhitelistedWebviewProps) {
const [status, requestPermission] = useTrackingPermissions();
const webviewRef = useRef<WebView | null>(null);
const navigation = useNavigation();

Expand Down Expand Up @@ -48,10 +50,18 @@ function WhitelistedWebview({ whitelistedUrls, ...props }: WhitelistedWebviewPro
}
}, [navigation]);

useEffect(() => {
(async () => {
await requestPermission();
})();
}, [requestPermission]);

return (
<WebView
{...props}
ref={webviewRef}
sharedCookiesEnabled={status?.granted}
thirdPartyCookiesEnabled={status?.granted}
injectedJavaScript={injectedJavascript}
onNavigationStateChange={(newNavState) => {
let foundMatch = whitelistedUrls.find((regex) => regex.test(newNavState.url));
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"expo-notifications": "~0.28.9",
"expo-device": "~6.0.2",
"react-native-webview": "13.8.6",
"expo-updates": "~0.25.21"
"expo-updates": "~0.25.21",
"expo-tracking-transparency": "~4.0.2"
},
"devDependencies": {
"@babel/core": "^7.20.0",
Expand Down
Loading