Skip to content

Commit 93aeeb1

Browse files
committed
Add unattributed outcomes for IAM click
* Add send outcomes and unique outcomes for button and image click * Add test for new case
1 parent ffe6b39 commit 93aeeb1

File tree

8 files changed

+466
-69
lines changed

8 files changed

+466
-69
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageAction.java

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,103 @@
33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
55

6+
import org.json.JSONArray;
67
import org.json.JSONException;
78
import org.json.JSONObject;
89

10+
import java.util.ArrayList;
11+
import java.util.List;
12+
913
public class OSInAppMessageAction {
10-
/** UUID assigned by OneSignal for internal use.
11-
* Package-private to track which element was tapped to report to the OneSignal dashboard. */
14+
15+
private static final String ID = "id";
16+
private static final String NAME = "name";
17+
private static final String URL = "url";
18+
private static final String URL_TARGET = "url_target";
19+
private static final String CLOSE = "close";
20+
private static final String CLICK_NAME = "click_name";
21+
private static final String CLICK_URL = "click_url";
22+
private static final String FIRST_CLICK = "first_click";
23+
private static final String CLOSES_MESSAGE = "closes_message";
24+
private static final String OUTCOMES = "outcomes";
25+
26+
/**
27+
* UUID assigned by OneSignal for internal use.
28+
* Package-private to track which element was tapped to report to the OneSignal dashboard.
29+
*/
1230
@NonNull
1331
String clickId;
1432

15-
/** An optional click name entered defined by the app developer when creating the IAM */
33+
/**
34+
* An optional click name entered defined by the app developer when creating the IAM
35+
*/
1636
@Nullable
1737
public String clickName;
1838

19-
/** Determines where the URL is opened, ie. Default browser. */
39+
/**
40+
* Determines where the URL is opened, ie. Default browser.
41+
*/
2042
@Nullable
2143
public OSInAppMessageActionUrlType urlTarget;
2244

23-
/** An optional URL that opens when the action takes place */
45+
/**
46+
* An optional URL that opens when the action takes place
47+
*/
2448
@Nullable
2549
public String clickUrl;
2650

27-
/** Determines if this was the first action taken on the in app message */
51+
/**
52+
* Outcome for action
53+
*/
54+
@NonNull
55+
public List<OSInAppMessageOutcome> outcomes = new ArrayList<>();
56+
57+
/**
58+
* Determines if this was the first action taken on the in app message
59+
*/
2860
public boolean firstClick;
2961

30-
/** Determines if tapping on the element should close the In-App Message. */
62+
/**
63+
* Determines if tapping on the element should close the In-App Message.
64+
*/
3165
public boolean closesMessage;
3266

33-
OSInAppMessageAction(@NonNull JSONObject json) {
34-
clickId = json.optString("id", null);
35-
clickName = json.optString("name", null);
36-
clickUrl = json.optString("url", null);
37-
urlTarget = OSInAppMessageActionUrlType.fromString(json.optString("url_target", null));
67+
OSInAppMessageAction(@NonNull JSONObject json) throws JSONException {
68+
clickId = json.optString(ID, null);
69+
clickName = json.optString(NAME, null);
70+
clickUrl = json.optString(URL, null);
71+
urlTarget = OSInAppMessageActionUrlType.fromString(json.optString(URL_TARGET, null));
3872
if (urlTarget == null)
3973
urlTarget = OSInAppMessageActionUrlType.IN_APP_WEBVIEW;
4074

41-
closesMessage = json.optBoolean("close", true);
75+
closesMessage = json.optBoolean(CLOSE, true);
76+
77+
if (json.has(OUTCOMES)) {
78+
JSONArray outcomesJsonArray = json.getJSONArray(OUTCOMES);
79+
for (int i = 0; i < outcomesJsonArray.length(); i++) {
80+
outcomes.add(new OSInAppMessageOutcome((JSONObject) outcomesJsonArray.get(i)));
81+
}
82+
}
4283
}
4384

4485
public JSONObject toJSONObject() {
4586
JSONObject mainObj = new JSONObject();
4687
try {
47-
mainObj.put("click_name", clickName);
48-
mainObj.put("click_url", clickUrl);
49-
mainObj.put("first_click", firstClick);
50-
mainObj.put("closes_message", closesMessage);
88+
mainObj.put(CLICK_NAME, clickName);
89+
mainObj.put(CLICK_URL, clickUrl);
90+
mainObj.put(FIRST_CLICK, firstClick);
91+
mainObj.put(CLOSES_MESSAGE, closesMessage);
92+
93+
JSONArray outcomesJson = new JSONArray();
94+
for (OSInAppMessageOutcome outcome : outcomes)
95+
outcomesJson.put(outcome.toJSONObject());
5196

97+
mainObj.put(OUTCOMES, outcomesJson);
5298
// Omitted for now until necessary
5399
// if (urlTarget != null)
54100
// mainObj.put("url_target", urlTarget.toJSONObject());
55101

56-
}
57-
catch(JSONException e) {
102+
} catch (JSONException e) {
58103
e.printStackTrace();
59104
}
60105

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSInAppMessageController.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,40 @@ void onFailure(int statusCode, String response, Throwable throwable) {
267267
}
268268
}
269269

270-
void onMessageActionOccurredOnMessage(@NonNull final OSInAppMessage message, @NonNull final JSONObject actionJson) {
270+
void onMessageActionOccurredOnMessage(@NonNull final OSInAppMessage message, @NonNull final JSONObject actionJson) throws JSONException {
271271
final OSInAppMessageAction action = new OSInAppMessageAction(actionJson);
272272
action.firstClick = message.takeActionAsUnique();
273273

274274
firePublicClickHandler(action);
275275
fireClickAction(action);
276276
fireRESTCallForClick(message, action);
277+
fireOutcomesForClick(action.outcomes);
277278
}
278279

279-
void onMessageActionOccurredOnPreview(@NonNull final OSInAppMessage message, @NonNull final JSONObject actionJson) {
280+
void onMessageActionOccurredOnPreview(@NonNull final OSInAppMessage message, @NonNull final JSONObject actionJson) throws JSONException {
280281
final OSInAppMessageAction action = new OSInAppMessageAction(actionJson);
281282
action.firstClick = message.takeActionAsUnique();
282283

283284
firePublicClickHandler(action);
284285
fireClickAction(action);
285286
}
286287

288+
//TODO This is a temporal solution for IAMs outcomes
289+
// and will potentially change when we track IAMs
290+
private void fireOutcomesForClick(@NonNull final List<OSInAppMessageOutcome> outcomes) {
291+
for (OSInAppMessageOutcome outcome : outcomes) {
292+
String name = outcome.getName();
293+
294+
if (outcome.isUnique()) {
295+
OneSignal.sendClickActionUniqueOutcome(name);
296+
} else if (outcome.getWeight() > 0) {
297+
OneSignal.sendClickActionOutcomeWithValue(name, outcome.getWeight());
298+
} else {
299+
OneSignal.sendClickActionOutcome(name);
300+
}
301+
}
302+
}
303+
287304
private void firePublicClickHandler(@NonNull final OSInAppMessageAction action) {
288305
if (OneSignal.mInitBuilder.mInAppMessageClickHandler == null)
289306
return;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.onesignal;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
public class OSInAppMessageOutcome {
9+
10+
private static final String OUTCOME_NAME = "name";
11+
private static final String OUTCOME_WEIGHT = "weight";
12+
private static final String OUTCOME_UNIQUE = "unique";
13+
14+
/**
15+
* Outcome key for action
16+
*/
17+
private String name;
18+
private float weight;
19+
private boolean unique;
20+
21+
OSInAppMessageOutcome(@NonNull JSONObject json) throws JSONException {
22+
name = json.getString(OUTCOME_NAME);
23+
weight = json.has(OUTCOME_WEIGHT) ? (float) json.getDouble(OUTCOME_WEIGHT) : 0;
24+
unique = json.has(OUTCOME_UNIQUE) && json.getBoolean(OUTCOME_UNIQUE);
25+
}
26+
27+
public JSONObject toJSONObject() {
28+
JSONObject mainObj = new JSONObject();
29+
try {
30+
mainObj.put(OUTCOME_NAME, name);
31+
mainObj.put(OUTCOME_WEIGHT, weight);
32+
mainObj.put(OUTCOME_UNIQUE, unique);
33+
} catch (JSONException e) {
34+
e.printStackTrace();
35+
}
36+
37+
return mainObj;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public float getWeight() {
49+
return weight;
50+
}
51+
52+
public void setWeight(float weight) {
53+
this.weight = weight;
54+
}
55+
56+
public boolean isUnique() {
57+
return unique;
58+
}
59+
60+
public void setUnique(boolean unique) {
61+
this.unique = unique;
62+
}
63+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OSSessionManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ private boolean willChangeSession(@NonNull Session session, @Nullable String dir
270270
.build();
271271
}
272272

273+
@NonNull SessionResult getIAMSessionResult() {
274+
if (OutcomesUtils.isUnattributedSessionEnabled()) {
275+
return SessionResult.Builder.newInstance()
276+
.setSession(Session.UNATTRIBUTED)
277+
.build();
278+
}
279+
280+
return SessionResult.Builder.newInstance()
281+
.setSession(Session.DISABLED)
282+
.build();
283+
}
284+
273285
/**
274286
* Attempt to override the current session before the 30 second session minimum
275287
* This should only be done in a upward direction:

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,19 @@ static OSSessionManager getSessionManager() {
30983098
return sessionManager;
30993099
}
31003100

3101+
static void sendClickActionOutcome(@NonNull String name) {
3102+
sendClickActionOutcomeWithValue(name, 0);
3103+
}
3104+
3105+
static void sendClickActionOutcomeWithValue(@NonNull String name, float value) {
3106+
if (outcomeEventsController == null) {
3107+
OneSignal.Log(LOG_LEVEL.ERROR, "Make sure OneSignal.init is called first");
3108+
return;
3109+
}
3110+
3111+
outcomeEventsController.sendClickOutcomeEventWithValue(name, value);
3112+
}
3113+
31013114
public static void sendOutcome(@NonNull String name) {
31023115
sendOutcome(name, null);
31033116
}
@@ -3114,6 +3127,15 @@ public static void sendOutcome(@NonNull String name, OutcomeCallback callback) {
31143127
outcomeEventsController.sendOutcomeEvent(name, callback);
31153128
}
31163129

3130+
static void sendClickActionUniqueOutcome(@NonNull String name) {
3131+
if (outcomeEventsController == null) {
3132+
OneSignal.Log(LOG_LEVEL.ERROR, "Make sure OneSignal.init is called first");
3133+
return;
3134+
}
3135+
3136+
outcomeEventsController.sendUniqueClickOutcomeEvent(name);
3137+
}
3138+
31173139
public static void sendUniqueOutcome(@NonNull String name) {
31183140
sendUniqueOutcome(name, null);
31193141
}

0 commit comments

Comments
 (0)