Skip to content

Commit 3cd9544

Browse files
author
Evan Masseau
committed
Added android "getCurrentGeofences" bridge function, tagged it internal
also added tests
1 parent 066427c commit 3cd9544

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

android/src/main/java/com/klaviyoreactnativesdk/KlaviyoReactNativeSdkModule.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.klaviyoreactnativesdk
22

3+
import com.facebook.react.bridge.Arguments
34
import com.facebook.react.bridge.Callback
45
import com.facebook.react.bridge.ReactApplicationContext
56
import com.facebook.react.bridge.ReactContextBaseJavaModule
@@ -20,6 +21,7 @@ import com.klaviyo.core.utils.AdvancedAPI
2021
import com.klaviyo.forms.InAppFormsConfig
2122
import com.klaviyo.forms.registerForInAppForms
2223
import com.klaviyo.forms.unregisterFromInAppForms
24+
import com.klaviyo.location.LocationManager
2325
import com.klaviyo.location.registerGeofencing
2426
import com.klaviyo.location.unregisterGeofencing
2527
import java.io.Serializable
@@ -99,6 +101,31 @@ class KlaviyoReactNativeSdkModule(
99101
Klaviyo.unregisterGeofencing()
100102
}
101103

104+
@ReactMethod
105+
fun getCurrentGeofences(callback: Callback) {
106+
// Note: in the future, we may be storing more fences than we are observing, so we'd have to update this
107+
val geofencesArray = Arguments.createArray()
108+
Registry.getOrNull<LocationManager>()?.getStoredGeofences()?.forEach { geofence ->
109+
geofencesArray.pushMap(
110+
Arguments.createMap().apply {
111+
putString("identifier", geofence.id)
112+
putDouble("latitude", geofence.latitude)
113+
putDouble("longitude", geofence.longitude)
114+
putDouble("radius", geofence.radius.toDouble())
115+
},
116+
)
117+
} ?: run {
118+
Registry.log.wtf("Geofencing is not yet registered")
119+
}
120+
121+
val resultMap =
122+
Arguments.createMap().apply {
123+
putArray("geofences", geofencesArray)
124+
}
125+
126+
callback.invoke(resultMap)
127+
}
128+
102129
@ReactMethod
103130
fun setProfile(profile: ReadableMap) {
104131
val parsedProfile = Profile()

example/src/KlaviyoReactWrapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export const unregisterGeofencing = async () => {
141141

142142
export const getCurrentGeofences = async () => {
143143
try {
144+
// Note: this @internal method is intended only for demonstration and debugging purposes
144145
Klaviyo.getCurrentGeofences((result) => {
145146
const { geofences } = result;
146147
console.log('Current geofences:', JSON.stringify(geofences, null, 2));

src/Geofencing.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,31 @@ export interface Geofence {
55
radius: number;
66
}
77

8+
/**
9+
* Interface for the Klaviyo location module
10+
*/
811
export interface KlaviyoGeofencingApi {
12+
/**
13+
* Begin geofence monitoring
14+
* Once proper permissions are granted from the user, we will fetch
15+
* geofences configured for your account and begin reporting
16+
* geofence transitions as analytics events.
17+
*/
918
registerGeofencing(): void;
19+
20+
/**
21+
* Stop monitoring geofences
22+
*/
1023
unregisterGeofencing(): void;
24+
25+
/**
26+
* Gets the currently monitored geofences from the native SDK.
27+
*
28+
* This is intended for demonstration and debugging purposes only.
29+
* It may be subject to change or removal without notice.
30+
*
31+
* @internal
32+
*/
1133
getCurrentGeofences(
1234
callback: (result: { geofences: Geofence[] }) => void
1335
): void;

src/__tests__/index.test.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jest.mock('react-native', () => {
2424
createEvent: jest.fn(),
2525
registerForInAppForms: jest.fn(),
2626
unregisterFromInAppForms: jest.fn(),
27+
registerGeofencing: jest.fn(),
28+
unregisterGeofencing: jest.fn(),
29+
getCurrentGeofences: jest.fn(),
2730
handleUniversalTrackingLink: jest.fn(),
2831
getConstants: jest.fn().mockReturnValue({
2932
PROFILE_KEYS: {
@@ -265,6 +268,56 @@ describe('Klaviyo SDK', () => {
265268
});
266269
});
267270

271+
describe('geofencing', () => {
272+
it('should register for geofencing', () => {
273+
Klaviyo.registerGeofencing();
274+
expect(
275+
NativeModules.KlaviyoReactNativeSdk.registerGeofencing
276+
).toHaveBeenCalledTimes(1);
277+
});
278+
279+
it('should unregister from geofencing', () => {
280+
Klaviyo.unregisterGeofencing();
281+
expect(
282+
NativeModules.KlaviyoReactNativeSdk.unregisterGeofencing
283+
).toHaveBeenCalledTimes(1);
284+
});
285+
286+
it('should get current geofences through callback correctly', () => {
287+
const mockGeofences = {
288+
geofences: [
289+
{
290+
identifier: 'geofence_1',
291+
latitude: 42.3601,
292+
longitude: -71.0589,
293+
radius: 100,
294+
},
295+
{
296+
identifier: 'geofence_2',
297+
latitude: 40.7128,
298+
longitude: -74.006,
299+
radius: 200,
300+
},
301+
],
302+
};
303+
304+
// Test with callback
305+
Klaviyo.getCurrentGeofences((result) => {
306+
expect(result).toEqual(mockGeofences);
307+
});
308+
309+
// Simulate callback invocation
310+
const getCurrentGeofencesCall =
311+
NativeModules.KlaviyoReactNativeSdk.getCurrentGeofences.mock
312+
.calls[0][0];
313+
getCurrentGeofencesCall(mockGeofences);
314+
315+
expect(
316+
NativeModules.KlaviyoReactNativeSdk.getCurrentGeofences
317+
).toHaveBeenCalled();
318+
});
319+
});
320+
268321
describe('tracking links', () => {
269322
it('should call the native handleUniversalTrackingLink method with a valid Klaviyo universal tracking link', () => {
270323
const trackingLink = 'https://klaviyo.com/u/abc123';

0 commit comments

Comments
 (0)