Skip to content

Commit d051d0f

Browse files
committed
Integration of getPlacement() method to Android bridge
1 parent 740d749 commit d051d0f

File tree

4 files changed

+172
-6
lines changed

4 files changed

+172
-6
lines changed

BridgeAPI.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,71 @@ None
293293

294294
### Example
295295
QubitSDK.experienceShown("https://sse.qubit.com/v1/callback?data=igK....n0=");
296+
297+
-------------------------------------------------------
298+
299+
## **getPlacement**(placementId, mode, attributes, campaignId, experienceId, placementPromise)
300+
301+
### Description
302+
Returns Placement for given parameters.
303+
304+
### Parameters
305+
- placementId
306+
- Type: String
307+
- Constraints: Not null
308+
- Description: Unique ID of the placement.
309+
- mode
310+
- Type: String
311+
- Constraints: Can be one of LIVE/SAMPLE/PREVIEW.
312+
- Description: The mode to fetch placements content with. Defaults to LIVE.
313+
- attributes
314+
- Type: String
315+
- Constraints: Should be string description of JSON or null
316+
- Description: JSON string containing custom attributes to be used to query for the placement. "visitor" attribute will be ignored as it is set by SDK.
317+
- campaignId
318+
- Type: String
319+
- Constraints: Nullable
320+
- Description: Campaign identifier
321+
- experienceId
322+
- Type: String
323+
- Constraints: Nullable
324+
- Description: Experience identifier
325+
- placementPromise
326+
- Type: Promise
327+
- Constraints: Not null
328+
- Description: Promise with query result
329+
330+
### Result
331+
Promise with a map describing Placement object. Example:
332+
333+
{
334+
"data": {
335+
"placementContent": {
336+
"content": {
337+
"image": "https://image.store.com/images/example.jpeg",
338+
"message": "Hello World",
339+
"url": "https://www.qubit.com"
340+
},
341+
"callbacks": {
342+
"impression": "https://api.qubit.com/placements/callback?data=ggW4eyJtZXRhIjp7ImlkIjo",
343+
"clickthrough": "https://api.qubit.com/placements/callback?data=mQW4eyJtZXRhIjp7Imlkx"
344+
}
345+
}
346+
}
347+
}
348+
349+
350+
### Exceptions
351+
- Exception is thrown, when SDK is not initialized.
352+
353+
### Example
354+
async () => {
355+
const placement = await getPlacement(
356+
"placement_id",
357+
"LIVE",
358+
"{ \"color\": \"blue\"}",
359+
"campaign_id",
360+
"experience_id"
361+
);
362+
...
363+
}

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,53 @@ async () => {
214214

215215
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<Experience>>** Promise with an array of Experience objects.
216216

217+
#### getPlacement
218+
219+
Returns Placement for given parameters.
220+
221+
##### Parameters
222+
223+
- `placementId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Unique ID of the placement.
224+
- `mode` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** The mode to fetch placements content with, can be one of LIVE/SAMPLE/PREVIEW. Defaults to LIVE.
225+
- `attributes` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** JSON string containing custom attributes to be used to query for the placement. "visitor" attribute will be ignored as it is set by SDK.
226+
- `campaignId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Optional.
227+
- `experienceId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Optional.
228+
- `placementPromise` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<Placement>** Promise with query result.
229+
230+
231+
##### Example
232+
233+
```javascript
234+
async () => {
235+
const placement = await getPlacement(
236+
"placement_id",
237+
"LIVE",
238+
"{ \"color\": \"blue\"}",
239+
"campaign_id",
240+
"experience_id"
241+
);
242+
...
243+
}
244+
245+
{
246+
"data": {
247+
"placementContent": {
248+
"content": {
249+
"image": "https://image.store.com/images/example.jpeg",
250+
"message": "Hello World",
251+
"url": "https://www.qubit.com"
252+
},
253+
"callbacks": {
254+
"impression": "https://api.qubit.com/placements/callback?data=ggW4eyJtZXRhIjp7ImlkIjo",
255+
"clickthrough": "https://api.qubit.com/placements/callback?data=mQW4eyJtZXRhIjp7Imlkx"
256+
}
257+
}
258+
}
259+
}
260+
```
261+
262+
Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<Placement>** with a map describing Placement object.
263+
217264
### Compatibility
218265

219266
Qubit SDK React Native is compatible with React Native 0.58 and higher

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ dependencies {
5858

5959
implementation "androidx.annotation:annotation:1.0.0"
6060
implementation "com.google.code.gson:gson:2.8.2"
61-
implementation 'com.qubit:qubit-sdk-android:1.4.1'
61+
implementation 'com.qubit:qubit-sdk-android:1.5.0'
6262
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0"
6363
}

android/src/main/java/com/qubit/reactnative/sdk/QubitSDKModule.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.qubit.reactnative.sdk;
22

33
import android.util.Log;
4-
import androidx.annotation.NonNull;
4+
55
import com.facebook.react.bridge.Promise;
66
import com.facebook.react.bridge.ReactApplicationContext;
77
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -12,18 +12,24 @@
1212
import com.google.gson.Gson;
1313
import com.google.gson.JsonArray;
1414
import com.google.gson.JsonObject;
15+
import com.google.gson.JsonParser;
1516
import com.qubit.android.sdk.api.QubitSDK;
1617
import com.qubit.android.sdk.api.logging.QBLogLevel;
18+
import com.qubit.android.sdk.api.placement.PlacementMode;
19+
import com.qubit.android.sdk.api.placement.PlacementPreviewOptions;
1720
import com.qubit.android.sdk.api.tracker.event.QBEvent;
1821
import com.qubit.android.sdk.api.tracker.event.QBEvents;
1922
import com.qubit.android.sdk.internal.experience.Experience;
20-
import com.qubit.android.sdk.internal.experience.callback.CallbackConnector;
21-
import com.qubit.android.sdk.internal.experience.callback.CallbackConnectorImpl;
23+
import com.qubit.android.sdk.internal.experience.callback.ExperienceCallbackConnector;
24+
import com.qubit.android.sdk.internal.experience.callback.ExperienceCallbackConnectorImpl;
2225
import com.qubit.android.sdk.internal.experience.model.ExperiencePayload;
2326
import com.qubit.android.sdk.internal.lookup.LookupData;
27+
2428
import java.util.ArrayList;
2529
import java.util.List;
2630

31+
import androidx.annotation.NonNull;
32+
2733

2834
public class QubitSDKModule extends ReactContextBaseJavaModule {
2935
private static ReactApplicationContext reactContext;
@@ -139,18 +145,63 @@ public void getExperiences(ReadableArray experienceIds,
139145

140146
@ReactMethod
141147
public void experienceShown(String callback) {
142-
CallbackConnector callbackConnector = new CallbackConnectorImpl(callback, QubitSDK.getDeviceId());
148+
ExperienceCallbackConnector callbackConnector = new ExperienceCallbackConnectorImpl(callback, QubitSDK.getDeviceId());
143149
callbackConnector.shown();
144150
}
145151

152+
@ReactMethod
153+
public void getPlacement(
154+
String placementId,
155+
String mode,
156+
String attributes,
157+
String campaignId,
158+
String experienceId,
159+
Promise placementPromise
160+
) {
161+
QubitSDK.getPlacement(
162+
placementId,
163+
matchMode(mode),
164+
getAttributesJson(attributes),
165+
new PlacementPreviewOptions(campaignId, experienceId),
166+
placement -> {
167+
WritableMap placementContentMap = WritableMapConverter.convertJsonToMap(placement.getContent());
168+
placementPromise.resolve(placementContentMap);
169+
return null;
170+
},
171+
throwable -> {
172+
placementPromise.reject(throwable);
173+
return null;
174+
}
175+
);
176+
}
177+
178+
private PlacementMode matchMode(String value) {
179+
switch (value) {
180+
case "SAMPLE":
181+
return PlacementMode.SAMPLE;
182+
case "PREVIEW":
183+
return PlacementMode.PREVIEW;
184+
case "LIVE":
185+
default:
186+
return PlacementMode.LIVE;
187+
}
188+
}
189+
190+
private JsonObject getAttributesJson(String attributes) {
191+
try {
192+
return new JsonParser().parse(attributes).getAsJsonObject();
193+
} catch (Exception e) {
194+
return null;
195+
}
196+
}
146197

147198
private static QBLogLevel defaultLogLevel = QBLogLevel.WARN;
148199

149200
private QBLogLevel parseLogLevel(String logLevel) {
150201
if (logLevel == null || logLevel.isEmpty()) {
151202
return defaultLogLevel;
152203
}
153-
for(QBLogLevel level : QBLogLevel.values()) {
204+
for (QBLogLevel level : QBLogLevel.values()) {
154205
if (level.toString().equalsIgnoreCase(logLevel))
155206
return level;
156207
}

0 commit comments

Comments
 (0)