Skip to content

Commit

Permalink
fix iOS segment adTracking issue
Browse files Browse the repository at this point in the history
  • Loading branch information
remicolin committed Jan 21, 2025
1 parent 44296d1 commit 8882422
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
70 changes: 48 additions & 22 deletions app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,63 @@ import * as amplitude from '@amplitude/analytics-react-native';
import { AMPLITUDE_KEY, SEGMENT_KEY } from '@env';

Check failure on line 10 in app/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'AMPLITUDE_KEY' is defined but never used
import { useToastController } from '@tamagui/toast';
import { YStack } from 'tamagui';
import { createClient } from '@segment/analytics-react-native';
import { requestTrackingPermission } from 'react-native-tracking-transparency';
import { createClient, EventPlugin, PluginType, SegmentEvent } from '@segment/analytics-react-native';

import MainScreen from './src/screens/MainScreen';
import useNavigationStore from './src/stores/navigationStore';
import useUserStore from './src/stores/userStore';
import { bgWhite } from './src/utils/colors';
import { setupUniversalLinkListener } from './src/utils/qrCode'; // Adjust the import path as needed

// Remove the segment client creation from here
// Instead export a function to create it
export const createSegmentClient = () =>
SEGMENT_KEY ? createClient({
export class DisableTrackingPlugin extends EventPlugin {
type = PluginType.before;

execute(event: SegmentEvent): SegmentEvent {
// Ensure context exists
if (!event.context) {
event.context = {};
}

// Ensure device context exists
if (!event.context.device) {
event.context.device = {};
}

// Force tracking related fields to be disabled
event.context.device.adTrackingEnabled = false;
event.context.device.advertisingId = undefined;
event.context.device.trackingStatus = 'not-authorized';
event.context.device.id = undefined;

return event;
}
}

export const createSegmentClient = () => {
if (!SEGMENT_KEY) return null;

Check warning on line 46 in app/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Expected { after 'if' condition

const client = createClient({
writeKey: SEGMENT_KEY,
trackAppLifecycleEvents: true,
trackDeepLinks: true,
debug: true,
}) : null;
collectDeviceId: false,
flushAt: 20,
defaultSettings: {
integrations: {
'Segment.io': {
apiKey: SEGMENT_KEY,
trackApplicationLifecycleEvents: false,
trackDeepLinks: false,
}

Check warning on line 61 in app/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
}

Check warning on line 62 in app/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
}

Check warning on line 63 in app/App.tsx

View workflow job for this annotation

GitHub Actions / lint

Missing trailing comma
});

client.add({ plugin: new DisableTrackingPlugin() });

return client;
};

// Export the client variable (will be initialized later)
export let segmentClient: ReturnType<typeof createClient> | null = null;
Expand Down Expand Up @@ -55,21 +94,8 @@ function App(): React.JSX.Element {
}, []);

useEffect(() => {
const requestTracking = async () => {
if (Platform.OS === 'ios') {
const status = await requestTrackingPermission();
console.log('Tracking permission status:', status);
// Initialize segment client after getting tracking permission
if (status === 'authorized') {
segmentClient = createSegmentClient();
}
} else {
// On Android, initialize directly
segmentClient = createSegmentClient();
}
};

requestTracking();
// Initialize segment directly without any tracking checks
segmentClient = createSegmentClient();
}, []);

return (
Expand Down
10 changes: 8 additions & 2 deletions app/ios/OpenPassport.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,10 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "$(inherited) DEBUG";
Expand Down Expand Up @@ -947,7 +950,10 @@
"-DFOLLY_MOBILE=1",
"-DFOLLY_USE_LIBCPP=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = false;
Expand Down
2 changes: 0 additions & 2 deletions app/ios/OpenPassport/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSUserTrackingUsageDescription</key>
<string>✨Help us improve the NFC passport reading experience. We only collect anonymous technical data to make passport scanning faster and more reliable for everyone.</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down

0 comments on commit 8882422

Please sign in to comment.