Skip to content

Commit d80afe7

Browse files
authored
Merge pull request #135 from jakecronin/master
Allow disable flush on background
2 parents 3bf1969 + d3bc472 commit d80afe7

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

__tests__/jest_setup.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jest.doMock('react-native', () => {
2020
initialize: jest.fn(),
2121
setServerURL: jest.fn(),
2222
setLoggingEnabled: jest.fn(),
23+
setFlushOnBackground: jest.fn(),
2324
setUseIpAddressForGeolocation: jest.fn(),
2425
hasOptedOutTracking: jest.fn(),
2526
optInTracking: jest.fn(),

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export class Mixpanel {
77
init(optOutTrackingDefault?: boolean, superProperties?: MixpanelProperties): Promise<void>;
88
setServerURL(serverURL: string): void;
99
setLoggingEnabled(loggingEnabled: boolean): void;
10+
setFlushOnBackground(flushOnBackground: boolean): void;
11+
1012
setUseIpAddressForGeolocation(useIpAddressForGeolocation: boolean): void;
1113
hasOptedOutTracking(): Promise<boolean>;
1214
optInTracking(): void;

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ export class Mixpanel {
106106
MixpanelReactNative.setLoggingEnabled(this.token, loggingEnabled);
107107
}
108108

109+
/**
110+
* This allows enabling or disabling whether or not Mixpanel flushes events
111+
* when the app enters the background on iOS. This is set to true by default.
112+
*
113+
* @param {boolean} flushOnBackground whether to enable logging
114+
*
115+
*/
116+
setFlushOnBackground(flushOnBackground) {
117+
if (Platform.OS === 'ios') {
118+
MixpanelReactNative.setFlushOnBackground(this.token, flushOnBackground);
119+
} else {
120+
console.warn('Mixpanel setFlushOnBackground was called and ignored because this method only works on iOS.')
121+
}
122+
}
123+
109124
/**
110125
* This controls whether to automatically send the client IP Address as part of event tracking.
111126
* With an IP address, geo-location is possible down to neighborhoods within a city,

ios/MixpanelReactNative.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ @interface RCT_EXTERN_MODULE(MixpanelReactNative, NSObject)
1212

1313
RCT_EXTERN_METHOD(setLoggingEnabled:(NSString *)token loggingEnabled:(BOOL)loggingEnabled resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
1414

15+
RCT_EXTERN_METHOD(setFlushOnBackground:(NSString *)token flushOnBackground:(BOOL)flushOnBackground resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
16+
1517
RCT_EXTERN_METHOD(setUseIpAddressForGeolocation:(NSString *)token useIpAddressForGeolocation:(BOOL)useIpAddressForGeolocation resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
1618

1719
// MARK: - Opting Users Out of Tracking

ios/MixpanelReactNative.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ open class MixpanelReactNative: NSObject {
4444
resolve(nil)
4545
}
4646

47+
@objc
48+
func setFlushOnBackground(_ token: String,
49+
flushOnBackground: Bool,
50+
resolver resolve: RCTPromiseResolveBlock,
51+
rejecter reject: RCTPromiseRejectBlock) -> Void {
52+
let instance = MixpanelReactNative.getMixpanelInstance(token)
53+
instance?.flushOnBackground = flushOnBackground
54+
resolve(nil)
55+
}
56+
4757
@objc
4858
func setUseIpAddressForGeolocation(_ token: String,
4959
useIpAddressForGeolocation: Bool,

0 commit comments

Comments
 (0)