Skip to content

Commit 44a8eff

Browse files
feat: Add shouldUploadEvent (#8)
1 parent 39e570e commit 44a8eff

File tree

5 files changed

+118
-4
lines changed

5 files changed

+118
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cordova plugin add cordova-plugin-mparticle
2020
**Install the SDK** using CocoaPods:
2121

2222
```bash
23-
$ # Update your Podfile to depend on 'mParticle-Apple-SDK' version 7.2.0 or later
23+
$ # Update your Podfile to depend on 'mParticle-Apple-SDK' version 8.5.2 or later
2424
$ pod install
2525
```
2626

plugin/src/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ dependencies {
2121
testCompile 'org.json:json:20080701'
2222
testCompile "org.mockito:mockito-core:1.+"
2323
provided 'org.apache.cordova:framework:7.1.0'
24-
provided 'com.mparticle:android-core:5.16.5'
24+
provided 'com.mparticle:android-core:5.24.0'
2525
}

plugin/src/android/src/main/java/com/mparticle/cordova/MParticleCordovaPlugin.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ public void logEvent(final JSONArray args) throws JSONException {
8686
MParticle.getInstance().logEvent(event);
8787
}
8888

89+
public void logMPEvent(final JSONArray args) throws JSONException {
90+
final JSONObject map = args.getJSONObject(0);
91+
if (map != null) {
92+
MPEvent event = ConvertMPEvent(map);
93+
MParticle.getInstance().logEvent(event);
94+
}
95+
}
96+
8997
public void logCommerceEvent(final JSONArray args) throws JSONException {
9098
final JSONObject map = args.getJSONObject(0);
9199
if (map != null) {
@@ -327,6 +335,41 @@ private static IdentityApiRequest ConvertIdentityAPIRequest(JSONObject map) thro
327335
return identityRequest.build();
328336
}
329337

338+
private static MPEvent ConvertMPEvent(ReadableMap map) throws JSONException {
339+
if ((map.hasKey("name")) && (map.hasKey("type"))) {
340+
String name = map.getString("name");
341+
Integer type = map.getInt("type");
342+
MPEvent.Builder builder = new MPEvent.Builder(name, ConvertEventType(type));
343+
if (map.hasKey("category")) {
344+
builder.category(map.getString("category"));
345+
}
346+
if (map.hasKey("duration")) {
347+
builder.duration(map.getDouble("duration"));
348+
}
349+
if (map.hasKey("info")) {
350+
ReadableMap customInfoMap = map.getMap("info");
351+
Map<String, String> customInfo = ConvertStringMap(customInfoMap);
352+
builder.info(customInfo);
353+
}
354+
if (map.hasKey("customFlags")) {
355+
ReadableMap customFlagsMap = map.getMap("customFlags");
356+
Map<String, String> customFlags = ConvertStringMap(customFlagsMap);
357+
for (Map.Entry<String, String> entry : customFlags.entrySet())
358+
{
359+
builder.addCustomFlag(entry.getKey(), entry.getValue());
360+
}
361+
}
362+
363+
if (map.hasKey("shouldUploadEvent")) {
364+
builder.shouldUploadEvent(map.getBoolean("shouldUploadEvent"));
365+
}
366+
367+
return builder.build();
368+
}
369+
Log.e(LOG_TAG, "Invalid event:" + map.toString());
370+
return null;
371+
}
372+
330373
private static CommerceEvent ConvertCommerceEvent(JSONObject map) throws JSONException {
331374
Boolean isProductAction = map.has("productActionType");
332375
Boolean isPromotion = map.has("promotionActionType");
@@ -382,6 +425,10 @@ else if (isPromotion) {
382425
}
383426
}
384427

428+
if (map.hasKey("shouldUploadEvent")) {
429+
builder.shouldUploadEvent(map.getBoolean("shouldUploadEvent"));
430+
}
431+
385432
return builder.build();
386433
}
387434

plugin/src/ios/CDVMParticle.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ - (void)logEvent:(CDVInvokedUrlCommand*)command {
1919
}];
2020
}
2121

22+
- (void)logMPEvent:(CDVInvokedUrlCommand*)command {
23+
[self.commandDelegate runInBackground:^{
24+
NSString *serializedEvent = [command.arguments objectAtIndex:0];
25+
26+
MPEvent *event = [CDVMParticle MPEvent:serializedEvent];
27+
28+
[[MParticle sharedInstance] logEvent:event];
29+
30+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
31+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
32+
}];
33+
}
34+
2235
- (void)logCommerceEvent:(CDVInvokedUrlCommand*)command {
2336
[self.commandDelegate runInBackground:^{
2437
NSString *serializedCommerceEvent = [command.arguments objectAtIndex:0];
2538

2639
MPCommerceEvent *commerceEvent = [CDVMParticle MPCommerceEvent:serializedCommerceEvent];
2740

28-
[[MParticle sharedInstance] logCommerceEvent:commerceEvent];
41+
[[MParticle sharedInstance] logEvent:commerceEvent];
2942

3043
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
3144
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
@@ -350,6 +363,9 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
350363
commerceEvent.checkoutStep = [json[@"checkoutStep"] intValue];
351364
}
352365
commerceEvent.nonInteractive = [json[@"nonInteractive"] boolValue];
366+
if (json[@"shouldUploadEvent"] != nil) {
367+
commerceEvent.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
368+
}
353369

354370
NSMutableArray *products = [NSMutableArray array];
355371
NSArray *jsonProducts = json[@"products"];
@@ -428,10 +444,13 @@ + (MPEvent *)MPEvent:(id)json {
428444
event.category = json[@"category"];
429445
event.duration = json[@"duration"];
430446
event.endTime = json[@"endTime"];
431-
event.info = json[@"info"];
447+
event.customAttributes = json[@"info"];
432448
event.name = json[@"name"];
433449
event.startTime = json[@"startTime"];
434450
event.type = [json[@"type"] integerValue];
451+
if (json[@"shouldUploadEvent"] != nil) {
452+
event.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
453+
}
435454

436455
NSDictionary *jsonFlags = json[@"customFlags"];
437456
for (NSString *key in jsonFlags) {

plugin/www/mparticle.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ var mparticle = {
9393
exec('logEvent', [eventName, type, attributes])
9494
},
9595

96+
logMPEvent: function (event) {
97+
exec('logMPEvent', [event])
98+
},
99+
96100
logCommerceEvent: function (commerceEvent) {
97101
exec('logCommerceEvent', [commerceEvent])
98102
},
@@ -183,6 +187,45 @@ var mparticle = {
183187
}
184188
},
185189

190+
Event: function () {
191+
this.setCategory = function (category) {
192+
this.category = category
193+
return this
194+
}
195+
this.setDuration = function (duration) {
196+
this.duration = duration
197+
return this
198+
}
199+
this.setEndTime = function (endTime) {
200+
this.endTime = endTime
201+
return this
202+
}
203+
this.setInfo = function (info) {
204+
this.info = info
205+
return this
206+
}
207+
this.setName = function (name) {
208+
this.name = name
209+
return this
210+
}
211+
this.setStartTime = function (startTime) {
212+
this.startTime = startTime
213+
return this
214+
}
215+
this.setType = function (type) {
216+
this.type = type
217+
return this
218+
}
219+
this.setShouldUploadEvent = function (shouldUploadEvent) {
220+
this.shouldUploadEvent = shouldUploadEvent
221+
return this
222+
}
223+
this.setCustomFlags = function (customFlags) {
224+
this.customFlags = customFlags
225+
return this
226+
}
227+
},
228+
186229
CommerceEvent: function () {
187230
this.setTransactionAttributes = function (transactionAttributes) {
188231
this.transactionAttributes = transactionAttributes
@@ -253,6 +296,11 @@ var mparticle = {
253296
this.nonInteractive = nonInteractive
254297
return this
255298
}
299+
300+
this.setShouldUploadEvent = function (shouldUploadEvent) {
301+
this.shouldUploadEvent = shouldUploadEvent
302+
return this
303+
}
256304
},
257305

258306
User: function (userId) {

0 commit comments

Comments
 (0)