Skip to content

Commit

Permalink
feat: Add shouldUploadEvent (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonStalnaker authored Sep 17, 2021
1 parent 39e570e commit 44a8eff
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cordova plugin add cordova-plugin-mparticle
**Install the SDK** using CocoaPods:

```bash
$ # Update your Podfile to depend on 'mParticle-Apple-SDK' version 7.2.0 or later
$ # Update your Podfile to depend on 'mParticle-Apple-SDK' version 8.5.2 or later
$ pod install
```

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ dependencies {
testCompile 'org.json:json:20080701'
testCompile "org.mockito:mockito-core:1.+"
provided 'org.apache.cordova:framework:7.1.0'
provided 'com.mparticle:android-core:5.16.5'
provided 'com.mparticle:android-core:5.24.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public void logEvent(final JSONArray args) throws JSONException {
MParticle.getInstance().logEvent(event);
}

public void logMPEvent(final JSONArray args) throws JSONException {
final JSONObject map = args.getJSONObject(0);
if (map != null) {
MPEvent event = ConvertMPEvent(map);
MParticle.getInstance().logEvent(event);
}
}

public void logCommerceEvent(final JSONArray args) throws JSONException {
final JSONObject map = args.getJSONObject(0);
if (map != null) {
Expand Down Expand Up @@ -327,6 +335,41 @@ private static IdentityApiRequest ConvertIdentityAPIRequest(JSONObject map) thro
return identityRequest.build();
}

private static MPEvent ConvertMPEvent(ReadableMap map) throws JSONException {
if ((map.hasKey("name")) && (map.hasKey("type"))) {
String name = map.getString("name");
Integer type = map.getInt("type");
MPEvent.Builder builder = new MPEvent.Builder(name, ConvertEventType(type));
if (map.hasKey("category")) {
builder.category(map.getString("category"));
}
if (map.hasKey("duration")) {
builder.duration(map.getDouble("duration"));
}
if (map.hasKey("info")) {
ReadableMap customInfoMap = map.getMap("info");
Map<String, String> customInfo = ConvertStringMap(customInfoMap);
builder.info(customInfo);
}
if (map.hasKey("customFlags")) {
ReadableMap customFlagsMap = map.getMap("customFlags");
Map<String, String> customFlags = ConvertStringMap(customFlagsMap);
for (Map.Entry<String, String> entry : customFlags.entrySet())
{
builder.addCustomFlag(entry.getKey(), entry.getValue());
}
}

if (map.hasKey("shouldUploadEvent")) {
builder.shouldUploadEvent(map.getBoolean("shouldUploadEvent"));
}

return builder.build();
}
Log.e(LOG_TAG, "Invalid event:" + map.toString());
return null;
}

private static CommerceEvent ConvertCommerceEvent(JSONObject map) throws JSONException {
Boolean isProductAction = map.has("productActionType");
Boolean isPromotion = map.has("promotionActionType");
Expand Down Expand Up @@ -382,6 +425,10 @@ else if (isPromotion) {
}
}

if (map.hasKey("shouldUploadEvent")) {
builder.shouldUploadEvent(map.getBoolean("shouldUploadEvent"));
}

return builder.build();
}

Expand Down
23 changes: 21 additions & 2 deletions plugin/src/ios/CDVMParticle.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,26 @@ - (void)logEvent:(CDVInvokedUrlCommand*)command {
}];
}

- (void)logMPEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *serializedEvent = [command.arguments objectAtIndex:0];

MPEvent *event = [CDVMParticle MPEvent:serializedEvent];

[[MParticle sharedInstance] logEvent:event];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}

- (void)logCommerceEvent:(CDVInvokedUrlCommand*)command {
[self.commandDelegate runInBackground:^{
NSString *serializedCommerceEvent = [command.arguments objectAtIndex:0];

MPCommerceEvent *commerceEvent = [CDVMParticle MPCommerceEvent:serializedCommerceEvent];

[[MParticle sharedInstance] logCommerceEvent:commerceEvent];
[[MParticle sharedInstance] logEvent:commerceEvent];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down Expand Up @@ -350,6 +363,9 @@ + (MPCommerceEvent *)MPCommerceEvent:(id)json {
commerceEvent.checkoutStep = [json[@"checkoutStep"] intValue];
}
commerceEvent.nonInteractive = [json[@"nonInteractive"] boolValue];
if (json[@"shouldUploadEvent"] != nil) {
commerceEvent.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
}

NSMutableArray *products = [NSMutableArray array];
NSArray *jsonProducts = json[@"products"];
Expand Down Expand Up @@ -428,10 +444,13 @@ + (MPEvent *)MPEvent:(id)json {
event.category = json[@"category"];
event.duration = json[@"duration"];
event.endTime = json[@"endTime"];
event.info = json[@"info"];
event.customAttributes = json[@"info"];
event.name = json[@"name"];
event.startTime = json[@"startTime"];
event.type = [json[@"type"] integerValue];
if (json[@"shouldUploadEvent"] != nil) {
event.shouldUploadEvent = [json[@"shouldUploadEvent"] boolValue];
}

NSDictionary *jsonFlags = json[@"customFlags"];
for (NSString *key in jsonFlags) {
Expand Down
48 changes: 48 additions & 0 deletions plugin/www/mparticle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ var mparticle = {
exec('logEvent', [eventName, type, attributes])
},

logMPEvent: function (event) {
exec('logMPEvent', [event])
},

logCommerceEvent: function (commerceEvent) {
exec('logCommerceEvent', [commerceEvent])
},
Expand Down Expand Up @@ -183,6 +187,45 @@ var mparticle = {
}
},

Event: function () {
this.setCategory = function (category) {
this.category = category
return this
}
this.setDuration = function (duration) {
this.duration = duration
return this
}
this.setEndTime = function (endTime) {
this.endTime = endTime
return this
}
this.setInfo = function (info) {
this.info = info
return this
}
this.setName = function (name) {
this.name = name
return this
}
this.setStartTime = function (startTime) {
this.startTime = startTime
return this
}
this.setType = function (type) {
this.type = type
return this
}
this.setShouldUploadEvent = function (shouldUploadEvent) {
this.shouldUploadEvent = shouldUploadEvent
return this
}
this.setCustomFlags = function (customFlags) {
this.customFlags = customFlags
return this
}
},

CommerceEvent: function () {
this.setTransactionAttributes = function (transactionAttributes) {
this.transactionAttributes = transactionAttributes
Expand Down Expand Up @@ -253,6 +296,11 @@ var mparticle = {
this.nonInteractive = nonInteractive
return this
}

this.setShouldUploadEvent = function (shouldUploadEvent) {
this.shouldUploadEvent = shouldUploadEvent
return this
}
},

User: function (userId) {
Expand Down

0 comments on commit 44a8eff

Please sign in to comment.