-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathIdfaPlugin.tsx
More file actions
80 lines (70 loc) · 2.15 KB
/
Copy pathIdfaPlugin.tsx
File metadata and controls
80 lines (70 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {
ErrorType,
Plugin,
PluginType,
SegmentClient,
SegmentError,
} from '@segment/analytics-react-native';
import type { IdfaData } from './types';
import { AnalyticsReactNativePluginIdfa } from './AnalyticsReactNativePluginIdfa';
import { Platform } from 'react-native';
const { getTrackingAuthorizationStatus } = AnalyticsReactNativePluginIdfa;
/**
* IDFA Plugin
* @constructor
* @param {boolean} shouldAskPermission - defaults to true. Passing false
* when initializing new `IDFA Plugin` will prevent plugin from
* requesting tracking status
*/
export class IdfaPlugin extends Plugin {
type = PluginType.enrichment;
private shouldAskPermission = true;
constructor(shouldAskPermission = true) {
super();
this.shouldAskPermission = shouldAskPermission;
}
configure(analytics: SegmentClient): void {
this.analytics = analytics;
if (Platform.OS !== 'ios') {
return;
}
if (this.shouldAskPermission === true) {
this.getTrackingStatus();
}
}
/** `requestTrackingPermission()` will prompt the user for
tracking permission and returns a promise you can use to
make additional tracking decisions based on the user response
*/
async requestTrackingPermission(): Promise<boolean> {
try {
const idfaData: IdfaData = await getTrackingAuthorizationStatus();
await this.analytics?.context.set({ device: { ...idfaData } });
return idfaData.adTrackingEnabled;
} catch (error) {
this.analytics?.reportInternalError(
new SegmentError(ErrorType.PluginError, JSON.stringify(error), error)
);
this.analytics?.logger.warn(error);
return false;
}
}
getTrackingStatus() {
return getTrackingAuthorizationStatus()
.then((idfa: IdfaData) => {
// update our context with the idfa data
void this.analytics?.context.set({ device: { ...idfa } });
return idfa;
})
.catch((error) => {
this.analytics?.reportInternalError(
new SegmentError(
ErrorType.PluginError,
'Error retreiving IDFA',
error
)
);
this.analytics?.logger.warn(error);
});
}
}