UNPKG

4.31 kBJavaScriptView Raw
1import { NativeModules, Platform } from 'react-native'
2
3const { RNBranch } = NativeModules
4
5import createBranchUniversalObject from './branchUniversalObject'
6import BranchEvent from './BranchEvent'
7import BranchSubscriber from './BranchSubscriber'
8
9const packageFile = require('./../package.json')
10export const VERSION = packageFile.version
11
12class Branch {
13 key = null;
14 _checkCachedEvents = true;
15 _debug = false;
16
17 constructor(options = {}) {
18 if (options.debug) this._debug = true
19
20 console.info('Initializing react-native-branch v. ' + VERSION)
21 }
22
23 subscribe(options) {
24 if (typeof options === 'function') {
25 /*
26 * Support for legacy API, passing a single callback function:
27 * branch.subscribe(({params, error, uri}) => { ... }). This is
28 * the same as the onOpenComplete callback.
29 */
30 options = {
31 onOpenComplete: options,
32 }
33 }
34
35 /*
36 * You can specify checkCachedEvents in the subscribe options to control
37 * this per subscriber.
38 */
39 if (!('checkCachedEvents' in options)) {
40 options.checkCachedEvents = this._checkCachedEvents
41 }
42 this._checkCachedEvents = false
43
44 const subscriber = new BranchSubscriber(options)
45 subscriber.subscribe()
46
47 return () => subscriber.unsubscribe()
48 }
49
50 skipCachedEvents() {
51 /*** Sets to ignore cached events. ***/
52 this._checkCachedEvents = false
53 }
54
55 /*** Tracking related methods ***/
56 disableTracking = (disable) => RNBranch.disableTracking(disable)
57 isTrackingDisabled = RNBranch.isTrackingDisabled
58
59 /*** RNBranch singleton methods ***/
60 setDebug = () => { throw 'setDebug() is not supported in the RN SDK. For other solutions, please see https://rnbranch.app.link/setDebug' }
61 getLatestReferringParams = (synchronous = false) => RNBranch.getLatestReferringParams(synchronous)
62 getFirstReferringParams = RNBranch.getFirstReferringParams
63 lastAttributedTouchData = (attributionWindow = {}) => RNBranch.lastAttributedTouchData(attributionWindow)
64 setIdentity = (identity) => RNBranch.setIdentity(identity)
65 setRequestMetadata = (key, value) => {
66 console.info('[Branch] setRequestMetadata has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the metadata.')
67 return RNBranch.setRequestMetadataKey(key, value)
68 }
69 addFacebookPartnerParameter = (name, value) => {
70 console.info('[Branch] addFacebookPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters.')
71 return RNBranch.addFacebookPartnerParameter(name, value)
72 }
73 clearPartnerParameter = RNBranch.clearPartnerParameter
74 logout = RNBranch.logout
75 userCompletedAction = (event, state = {}) => RNBranch.userCompletedAction(event, state)
76 getShortUrl = RNBranch.getShortUrl
77 sendCommerceEvent = (revenue, metadata) => {
78 console.info('[Branch] sendCommerceEvent is deprecated. Please use the BranchEvent class instead.')
79 return RNBranch.sendCommerceEvent('' + revenue, metadata)
80 }
81 openURL = (url, options = {}) => {
82 return Platform.select({
83 android: () => RNBranch.openURL(url, options),
84 ios: () => RNBranch.openURL(url)
85 })()
86 }
87 handleATTAuthorizationStatus = (ATTAuthorizationStatus) => {
88 if (Platform.OS != 'ios') return;
89 let normalizedAttAuthorizationStatus = -1
90
91 switch(ATTAuthorizationStatus) {
92 case 'authorized':
93 normalizedAttAuthorizationStatus = 3;
94 break;
95 case 'denied':
96 normalizedAttAuthorizationStatus = 2;
97 break;
98 case 'undetermined':
99 normalizedAttAuthorizationStatus = 0;
100 break;
101 case 'restricted':
102 normalizedAttAuthorizationStatus = 1;
103 break;
104 }
105
106 if (normalizedAttAuthorizationStatus < 0) {
107 console.info('[Branch] handleATTAuthorizationStatus received an unrecognized value. Value must be one of; authorized, denied, undetermined, or restricted')
108 return;
109 }
110
111 RNBranch.handleATTAuthorizationStatus(normalizedAttAuthorizationStatus)
112 }
113
114 /*** BranchUniversalObject ***/
115 createBranchUniversalObject = createBranchUniversalObject
116}
117
118export { Branch, BranchEvent, BranchSubscriber }
119export default new Branch()
\No newline at end of file