Skip to content

Commit

Permalink
Merge pull request #219 from mixpanel/zihe-flushBatchSize
Browse files Browse the repository at this point in the history
add api: setFlushBatchSize
  • Loading branch information
zihejia authored Dec 2, 2023
2 parents 780c4ce + 17e6ef4 commit 5a8e5dc
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion MixpanelReactNative.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ Pod::Spec.new do |s|
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }

s.dependency "React-Core"
s.dependency "Mixpanel-swift", '4.1.3'
s.dependency "Mixpanel-swift", '4.2.0'
end
6 changes: 6 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ test(`it calls MixpanelReactNative setUseIpAddressForGeolocation`, async () => {
expect(NativeModules.MixpanelReactNative.setUseIpAddressForGeolocation).toBeCalledWith("token", true);
});

test(`it calls MixpanelReactNative setFlushBatchSize`, async () => {
const mixpanel = await Mixpanel.init("token", true);
mixpanel.setFlushBatchSize(20);
expect(NativeModules.MixpanelReactNative.setFlushBatchSize).toBeCalledWith("token", 20);
});

test(`it calls MixpanelReactNative hasOptedOutTracking`, async () => {
const mixpanel = await Mixpanel.init("token", true);
mixpanel.hasOptedOutTracking();
Expand Down
1 change: 1 addition & 0 deletions __tests__/jest_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jest.doMock('react-native', () => {
setLoggingEnabled: jest.fn(),
setFlushOnBackground: jest.fn(),
setUseIpAddressForGeolocation: jest.fn(),
setFlushBatchSize: jest.fn(),
hasOptedOutTracking: jest.fn(),
optInTracking: jest.fn(),
optOutTracking: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ repositories {

dependencies {
implementation 'com.facebook.react:react-native:+'
implementation 'com.mixpanel.android:mixpanel-android:7.3.1'
implementation 'com.mixpanel.android:mixpanel-android:7.3.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void setServerURL(final String token, final String serverURL, Promise pro
}

@ReactMethod
public void setUseIpAddressForGeolocation(final String token, boolean useIpAddressForGeolocation, Promise promise) throws JSONException {
public void setUseIpAddressForGeolocation(final String token, boolean useIpAddressForGeolocation, Promise promise)
throws JSONException {
MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token, true);
if (instance == null) {
promise.reject("Instance Error", "Failed to get Mixpanel instance");
Expand All @@ -67,6 +68,19 @@ public void setUseIpAddressForGeolocation(final String token, boolean useIpAddre
}
}

@ReactMethod
public void setFlushBatchSize(final String token, Integer flushBatchSize, Promise promise) throws JSONException {
MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token, true);
if (instance == null) {
promise.reject("Instance Error", "Failed to get Mixpanel instance");
return;
}
synchronized (instance) {
instance.setFlushBatchSize(flushBatchSize);
promise.resolve(null);
}
}

@ReactMethod
public void setLoggingEnabled(final String token, boolean enableLogging, Promise promise) throws JSONException {
MixpanelAPI instance = MixpanelAPI.getInstance(this.mReactContext, token, true);
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class Mixpanel {
setServerURL(serverURL: string): void;
setLoggingEnabled(loggingEnabled: boolean): void;
setFlushOnBackground(flushOnBackground: boolean): void;

setUseIpAddressForGeolocation(useIpAddressForGeolocation: boolean): void;
setFlushBatchSize(flushBatchSize: number): void;
hasOptedOutTracking(): Promise<boolean>;
optInTracking(): void;
optOutTracking(): void;
Expand Down
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ export class Mixpanel {
setUseIpAddressForGeolocation(useIpAddressForGeolocation) {
MixpanelReactNative.setUseIpAddressForGeolocation(this.token, useIpAddressForGeolocation);
}

/**
* Set the number of events sent in a single network request to the Mixpanel server.
* By configuring this value, you can optimize network usage and manage the frequency of communication between the client and the server. The maximum size is 50; any value over 50 will default to 50.
*
* @param {integer} flushBatchSize whether to automatically send the client IP Address.
* Defaults to true.
*
*/
setFlushBatchSize(flushBatchSize) {
MixpanelReactNative.setFlushBatchSize(this.token, flushBatchSize);
}


/**
* Will return true if the user has opted out from tracking.
Expand Down
2 changes: 2 additions & 0 deletions ios/MixpanelReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ @interface RCT_EXTERN_MODULE(MixpanelReactNative, NSObject)

RCT_EXTERN_METHOD(setUseIpAddressForGeolocation:(NSString *)token useIpAddressForGeolocation:(BOOL)useIpAddressForGeolocation resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(setFlushBatchSize:(NSString *)token flushBatchSize:(NSInteger)flushBatchSize resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)

// MARK: - Opting Users Out of Tracking

RCT_EXTERN_METHOD(optOutTracking:(NSString *)token resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
Expand Down
10 changes: 10 additions & 0 deletions ios/MixpanelReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ open class MixpanelReactNative: NSObject {
resolve(nil)
}

@objc
func setFlushBatchSize(_ token: String,
flushBatchSize: Int,
resolver resolve: RCTPromiseResolveBlock,
rejecter reject: RCTPromiseRejectBlock) -> Void {
let instance = MixpanelReactNative.getMixpanelInstance(token)
instance?.flushBatchSize = flushBatchSize
resolve(nil)
}

@objc
func setUseIpAddressForGeolocation(_ token: String,
useIpAddressForGeolocation: Bool,
Expand Down

0 comments on commit 5a8e5dc

Please sign in to comment.