Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration of getPlacement() method to Android bridge #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions BridgeAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,71 @@ None

### Example
QubitSDK.experienceShown("https://sse.qubit.com/v1/callback?data=igK....n0=");

-------------------------------------------------------

## **getPlacement**(placementId, mode, attributes, campaignId, experienceId, placementPromise)

### Description
Returns Placement for given parameters.

### Parameters
- placementId
- Type: String
- Constraints: Not null
- Description: Unique ID of the placement.
- mode
- Type: String
- Constraints: Can be one of LIVE/SAMPLE/PREVIEW.
- Description: The mode to fetch placements content with. Defaults to LIVE.
- attributes
- Type: String
- Constraints: Should be string description of JSON or null
- 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.
- campaignId
- Type: String
- Constraints: Nullable
- Description: Campaign identifier
- experienceId
- Type: String
- Constraints: Nullable
- Description: Experience identifier
- placementPromise
- Type: Promise
- Constraints: Not null
- Description: Promise with query result

### Result
Promise with a map describing Placement object. Example:

{
"data": {
"placementContent": {
"content": {
"image": "https://image.store.com/images/example.jpeg",
"message": "Hello World",
"url": "https://www.qubit.com"
},
"callbacks": {
"impression": "https://api.qubit.com/placements/callback?data=ggW4eyJtZXRhIjp7ImlkIjo",
"clickthrough": "https://api.qubit.com/placements/callback?data=mQW4eyJtZXRhIjp7Imlkx"
}
}
}
}


### Exceptions
- Exception is thrown, when SDK is not initialized.

### Example
async () => {
const placement = await getPlacement(
"placement_id",
"LIVE",
"{ \"color\": \"blue\"}",
"campaign_id",
"experience_id"
);
...
}
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,53 @@ async () => {

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.

#### getPlacement

Returns Placement for given parameters.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method getPlacement should be added to the Typescript definition of API in /src/index.ts


##### Parameters

- `placementId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Unique ID of the placement.
- `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.
- `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.
- `campaignId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Optional.
- `experienceId` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** Optional.
- `placementPromise` **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<Placement>** Promise with query result.


##### Example

```javascript
async () => {
const placement = await getPlacement(
"placement_id",
"LIVE",
"{ \"color\": \"blue\"}",
"campaign_id",
"experience_id"
);
...
}

{
"data": {
"placementContent": {
"content": {
"image": "https://image.store.com/images/example.jpeg",
"message": "Hello World",
"url": "https://www.qubit.com"
},
"callbacks": {
"impression": "https://api.qubit.com/placements/callback?data=ggW4eyJtZXRhIjp7ImlkIjo",
"clickthrough": "https://api.qubit.com/placements/callback?data=mQW4eyJtZXRhIjp7Imlkx"
}
}
}
}
```

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<Placement>** with a map describing Placement object.

### Compatibility

Qubit SDK React Native is compatible with React Native 0.58 and higher
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ dependencies {

implementation "androidx.annotation:annotation:1.0.0"
implementation "com.google.code.gson:gson:2.8.2"
implementation 'com.qubit:qubit-sdk-android:1.4.1'
implementation 'com.qubit:qubit-sdk-android:1.5.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.qubit.reactnative.sdk;

import android.util.Log;
import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand All @@ -12,18 +12,24 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.qubit.android.sdk.api.QubitSDK;
import com.qubit.android.sdk.api.logging.QBLogLevel;
import com.qubit.android.sdk.api.placement.PlacementMode;
import com.qubit.android.sdk.api.placement.PlacementPreviewOptions;
import com.qubit.android.sdk.api.tracker.event.QBEvent;
import com.qubit.android.sdk.api.tracker.event.QBEvents;
import com.qubit.android.sdk.internal.experience.Experience;
import com.qubit.android.sdk.internal.experience.callback.CallbackConnector;
import com.qubit.android.sdk.internal.experience.callback.CallbackConnectorImpl;
import com.qubit.android.sdk.internal.experience.callback.ExperienceCallbackConnector;
import com.qubit.android.sdk.internal.experience.callback.ExperienceCallbackConnectorImpl;
import com.qubit.android.sdk.internal.experience.model.ExperiencePayload;
import com.qubit.android.sdk.internal.lookup.LookupData;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;


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

@ReactMethod
public void experienceShown(String callback) {
CallbackConnector callbackConnector = new CallbackConnectorImpl(callback, QubitSDK.getDeviceId());
ExperienceCallbackConnector callbackConnector = new ExperienceCallbackConnectorImpl(callback, QubitSDK.getDeviceId());
callbackConnector.shown();
}

@ReactMethod
public void getPlacement(
String placementId,
String mode,
String attributes,
String campaignId,
String experienceId,
Promise placementPromise
) {
QubitSDK.getPlacement(
placementId,
matchMode(mode),
getAttributesJson(attributes),
new PlacementPreviewOptions(campaignId, experienceId),
placement -> {
WritableMap placementContentMap = WritableMapConverter.convertJsonToMap(placement.getContent());
placementPromise.resolve(placementContentMap);
return null;
},
throwable -> {
placementPromise.reject(throwable);
return null;
}
);
}

private PlacementMode matchMode(String value) {
switch (value) {
case "SAMPLE":
return PlacementMode.SAMPLE;
case "PREVIEW":
return PlacementMode.PREVIEW;
case "LIVE":
default:
return PlacementMode.LIVE;
}
}

private JsonObject getAttributesJson(String attributes) {
try {
return new JsonParser().parse(attributes).getAsJsonObject();
} catch (Exception e) {
return null;
}
}

private static QBLogLevel defaultLogLevel = QBLogLevel.WARN;

private QBLogLevel parseLogLevel(String logLevel) {
if (logLevel == null || logLevel.isEmpty()) {
return defaultLogLevel;
}
for(QBLogLevel level : QBLogLevel.values()) {
for (QBLogLevel level : QBLogLevel.values()) {
if (level.toString().equalsIgnoreCase(logLevel))
return level;
}
Expand Down