Skip to content

Commit 3b8c8aa

Browse files
authored
Merge pull request #7 from Iterable/feature/SI-92-send-via-sdk
Feature/si 92 send via sdk
2 parents 40d6f2f + 3a395dd commit 3b8c8aa

File tree

2 files changed

+107
-51
lines changed

2 files changed

+107
-51
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class IterableApi {
2727
private Context _applicationContext;
2828
private String _apiKey;
2929
private String _email;
30+
private String _userId;
3031
private boolean _debugMode;
3132
private Bundle _payloadData;
3233
private IterableNotificationData _notificationData;
@@ -39,9 +40,6 @@ public class IterableApi {
3940
IterableApi(){
4041
}
4142

42-
IterableApi(Context context, String apiKey, String email){
43-
updateData(context, apiKey, email);
44-
}
4543
//---------------------------------------------------------------------------------------
4644
//endregion
4745

@@ -104,6 +102,60 @@ void setNotificationData(IterableNotificationData data) {
104102
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
105103
* Should be called whenever the app is opened.
106104
* @param currentActivity The current activity
105+
* @param userId The current userId
106+
* @return stored instance of IterableApi
107+
*/
108+
public static IterableApi sharedInstanceWithApiKeyWithUserId(Activity currentActivity, String apiKey,
109+
String userId)
110+
{
111+
return sharedInstanceWithApiKeyWithUserId(currentActivity, apiKey, userId, false);
112+
}
113+
114+
/**
115+
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
116+
* Should be called whenever the app is opened.
117+
* Allows the IterableApi to be intialized with debugging enabled
118+
* @param currentActivity The current activity
119+
* @param userId
120+
* The current userId@return stored instance of IterableApi
121+
*/
122+
public static IterableApi sharedInstanceWithApiKeyWithUserId(Activity currentActivity, String apiKey,
123+
String userId, boolean debugMode)
124+
{
125+
return sharedInstanceWithApiKeyWithUserId((Context) currentActivity, apiKey, userId, debugMode);
126+
}
127+
128+
/**
129+
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
130+
* Should be called whenever the app is opened.
131+
* @param currentContext The current context
132+
* @param userId The current userId
133+
* @return stored instance of IterableApi
134+
*/
135+
public static IterableApi sharedInstanceWithApiKeyWithUserId(Context currentContext, String apiKey,
136+
String userId)
137+
{
138+
return sharedInstanceWithApiKey(currentContext, apiKey, null, userId, false);
139+
}
140+
141+
/**
142+
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
143+
* Should be called whenever the app is opened.
144+
* Allows the IterableApi to be intialized with debugging enabled
145+
* @param currentContext The current context
146+
* @return stored instance of IterableApi
147+
*/
148+
public static IterableApi sharedInstanceWithApiKeyWithUserId(Context currentContext, String apiKey,
149+
String userId, boolean debugMode)
150+
{
151+
return sharedInstanceWithApiKey(currentContext, apiKey, null, userId, debugMode);
152+
}
153+
154+
/**
155+
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
156+
* Should be called whenever the app is opened.
157+
* @param currentActivity The current activity
158+
* @param email The current email
107159
* @return stored instance of IterableApi
108160
*/
109161
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey,
@@ -117,6 +169,7 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
117169
* Should be called whenever the app is opened.
118170
* Allows the IterableApi to be intialized with debugging enabled
119171
* @param currentActivity The current activity
172+
* @param email The current email
120173
* @return stored instance of IterableApi
121174
*/
122175
public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, String apiKey,
@@ -128,26 +181,34 @@ public static IterableApi sharedInstanceWithApiKey(Activity currentActivity, Str
128181
/**
129182
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
130183
* Should be called whenever the app is opened.
131-
* @param currentActivity The current activity
184+
* @param currentContext The current context
185+
* @param email The current email
132186
* @return stored instance of IterableApi
133187
*/
134-
public static IterableApi sharedInstanceWithApiKey(Context currentActivity, String apiKey,
188+
public static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
135189
String email)
136190
{
137-
return sharedInstanceWithApiKey(currentActivity, apiKey, email, false);
191+
return sharedInstanceWithApiKey(currentContext, apiKey, email, false);
138192
}
139193

140194
/**
141195
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
142196
* Should be called whenever the app is opened.
143197
* Allows the IterableApi to be intialized with debugging enabled
144198
* @param currentContext The current context
199+
* @param email The current email
145200
* @return stored instance of IterableApi
146201
*/
147202
public static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
148203
String email, boolean debugMode)
149204
{
150-
sharedInstance.updateData(currentContext.getApplicationContext(), apiKey, email);
205+
return sharedInstanceWithApiKey(currentContext, apiKey, email, null, debugMode);
206+
}
207+
208+
private static IterableApi sharedInstanceWithApiKey(Context currentContext, String apiKey,
209+
String email, String userId, boolean debugMode)
210+
{
211+
sharedInstance.updateData(currentContext.getApplicationContext(), apiKey, email, userId);
151212

152213
if (currentContext instanceof Activity) {
153214
Activity currentActivity = (Activity) currentContext;
@@ -217,7 +278,7 @@ public void track(String eventName, String campaignId, String templateId) {
217278
public void track(String eventName, String campaignId, String templateId, JSONObject dataFields) {
218279
JSONObject requestJSON = new JSONObject();
219280
try {
220-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
281+
addEmailOrUserIdToJson(requestJSON);
221282
requestJSON.put(IterableConstants.KEY_EVENTNAME, eventName);
222283

223284
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
@@ -231,32 +292,9 @@ public void track(String eventName, String campaignId, String templateId, JSONOb
231292
sendRequest(IterableConstants.ENDPOINT_TRACK, requestJSON);
232293
}
233294

234-
public void trackConversion(int campaignId, int templateId) {
235-
trackConversion(campaignId, templateId, null);
236-
}
237-
238-
public void trackConversion(int campaignId, int templateId, JSONObject dataFields) {
239-
240-
JSONObject requestJSON = new JSONObject();
241-
242-
try {
243-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
244-
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
245-
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
246-
if (dataFields != null) {
247-
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
248-
}
249-
}
250-
catch (JSONException e) {
251-
e.printStackTrace();
252-
}
253-
254-
sendRequest(IterableConstants.ENDPOINT_TRACKCONVERSION, requestJSON);
255-
}
256-
257295
public void sendPush(String email, int campaignId) {
258-
sendPush(email, campaignId, null, null);
259-
}
296+
sendPush(email, campaignId, null, null);
297+
}
260298

261299
/**
262300
* Sends a push campaign to an email address at the given time.
@@ -300,26 +338,31 @@ public void sendPush(String email, int campaignId, Date sendAt, JSONObject dataF
300338
}
301339

302340
public void updateEmail(String newEmail) {
303-
JSONObject requestJSON = new JSONObject();
341+
if (_email != null) {
342+
JSONObject requestJSON = new JSONObject();
304343

305-
try {
306-
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
307-
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
308-
}
309-
catch (JSONException e) {
310-
e.printStackTrace();
311-
}
344+
try {
345+
requestJSON.put(IterableConstants.KEY_CURRENT_EMAIL, _email);
346+
requestJSON.put(IterableConstants.KEY_NEW_EMAIL, newEmail);
347+
}
348+
catch (JSONException e) {
349+
e.printStackTrace();
350+
}
312351

313-
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
352+
sendRequest(IterableConstants.ENDPOINT_UPDATEEMAIL, requestJSON);
314353

315-
_email = newEmail;
354+
_email = newEmail;
355+
} else {
356+
IterableLogger.w(TAG, "updateEmail should not be called with a userId. " +
357+
"Init SDK with sharedInstanceWithApiKey instead of sharedInstanceWithApiKeyWithUserId");
358+
}
316359
}
317360

318361
public void updateUser(JSONObject dataFields) {
319362
JSONObject requestJSON = new JSONObject();
320363

321364
try {
322-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
365+
addEmailOrUserIdToJson(requestJSON);
323366
requestJSON.put(IterableConstants.KEY_DATAFIELDS, dataFields);
324367
}
325368
catch (JSONException e) {
@@ -373,7 +416,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
373416
JSONObject requestJSON = new JSONObject();
374417

375418
try {
376-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
419+
addEmailOrUserIdToJson(requestJSON);
377420
requestJSON.put(IterableConstants.KEY_CAMPAIGNID, campaignId);
378421
requestJSON.put(IterableConstants.KEY_TEMPLATE_ID, templateId);
379422
requestJSON.put(IterableConstants.KEY_MESSAGE_ID, messageId);
@@ -392,8 +435,7 @@ protected void trackPushOpen(int campaignId, int templateId, String messageId) {
392435
protected void disablePush(String token) {
393436
JSONObject requestJSON = new JSONObject();
394437
try {
395-
requestJSON.put(IterableConstants.KEY_TOKEN, token);
396-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
438+
requestJSON.put(IterableConstants.KEY_TOKEN, token);addEmailOrUserIdToJson(requestJSON);
397439
}
398440
catch (JSONException e) {
399441
e.printStackTrace();
@@ -406,10 +448,11 @@ protected void disablePush(String token) {
406448

407449
//region Private Fuctions
408450
//---------------------------------------------------------------------------------------
409-
private void updateData(Context context, String apiKey, String email) {
451+
private void updateData(Context context, String apiKey, String email, String userId) {
410452
this._applicationContext = context;
411453
this._apiKey = apiKey;
412454
this._email = email;
455+
this._userId = userId;
413456
}
414457

415458
private void tryTrackNotifOpen(Intent calledIntent) {
@@ -434,7 +477,8 @@ private void registerDeviceToken(String applicationName, String token, JSONObjec
434477

435478
JSONObject requestJSON = new JSONObject();
436479
try {
437-
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
480+
addEmailOrUserIdToJson(requestJSON);
481+
438482
if (dataFields == null) {
439483
dataFields = new JSONObject();
440484
}
@@ -463,6 +507,18 @@ private void sendRequest(String resourcePath, JSONObject json) {
463507
new IterableRequest().execute(request);
464508
}
465509

510+
private void addEmailOrUserIdToJson(JSONObject requestJSON) {
511+
try {
512+
if (_email != null) {
513+
requestJSON.put(IterableConstants.KEY_EMAIL, _email);
514+
} else {
515+
requestJSON.put(IterableConstants.KEY_USER_ID, _userId);
516+
}
517+
} catch (JSONException e) {
518+
e.printStackTrace();
519+
}
520+
}
521+
466522
//---------------------------------------------------------------------------------------
467523
//endregion
468524

iterableapi/src/main/java/com/iterable/iterableapi/IterableConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public final class IterableConstants {
2020
public static final String KEY_RECIPIENT_EMAIL = "recipientEmail";
2121
public static final String KEY_SEND_AT = "sendAt";
2222
public static final String KEY_TEMPLATE_ID = "templateId";
23-
public static final String KEY_MESSAGE_ID = "messageId";
23+
public static final String KEY_MESSAGE_ID = "messageId";
2424
public static final String KEY_TOKEN = "token";
25+
public static final String KEY_USER_ID = "userId";
2526
public static final String KEY_PLATFORM = "platform";
2627
public static final String KEY_APPLICATIONNAME = "applicationName";
2728
public static final String KEY_DEVICE = "device";
@@ -33,7 +34,6 @@ public final class IterableConstants {
3334
public static final String ENDPOINT_PUSHTARGET = "push/target";
3435
public static final String ENDPOINT_REGISTERDEVICETOKEN = "users/registerDeviceToken";
3536
public static final String ENDPOINT_TRACK = "events/track";
36-
public static final String ENDPOINT_TRACKCONVERSION = "events/trackConversion";
3737
public static final String ENDPOINT_TRACKPURCHASE = "commerce/trackPurchase";
3838
public static final String ENDPOINT_TRACKPUSHOPEN = "events/trackPushOpen";
3939
public static final String ENDPOINT_UPDATEEMAIL = "users/updateEmail";

0 commit comments

Comments
 (0)