Skip to content
This repository was archived by the owner on Jul 28, 2022. It is now read-only.

Commit 56f12bb

Browse files
committed
Add date event attribute
(user attribute already implemented)
1 parent 8caa194 commit 56f12bb

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

android/src/main/java/tech/bam/RNBatchPush/RNUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.json.JSONArray;
1717

1818
import java.net.URI;
19+
import java.util.Date;
1920
import java.util.Map;
2021

2122
public class RNUtils {
@@ -37,6 +38,8 @@ public static WritableMap convertMapToWritableMap(Map<String, Object> input) {
3738
output.putDouble(key, (Double) value);
3839
} else if (value instanceof String) {
3940
output.putString(key, (String) value);
41+
} else if (value instanceof Date) {
42+
output.putDouble(key, ((Date) value).getTime());
4043
} else if (value instanceof URI) {
4144
output.putString(key, value.toString());
4245
} else {
@@ -63,6 +66,8 @@ public static WritableArray convertArrayToWritableArray(Object[] input) {
6366
output.pushDouble((Double) value);
6467
} else if (value instanceof String) {
6568
output.pushString((String) value);
69+
} else if (value instanceof Date) {
70+
output.pushDouble(((Date) value).getTime());
6671
} else if (value instanceof URI) {
6772
output.pushString(value.toString());
6873
} else {
@@ -100,6 +105,10 @@ public static BatchEventData convertSerializedEventDataToEventData(@Nullable Rea
100105
batchEventData.put(key, valueMap.getDouble("value"));
101106
} else if ("float".equals(type)) {
102107
batchEventData.put(key, valueMap.getDouble("value"));
108+
} else if ("date".equals(type)) {
109+
long timestamp = (long) valueMap.getDouble("value");
110+
Date date = new Date(timestamp);
111+
batchEventData.put(key, date);
103112
} else if ("url".equals(type)) {
104113
batchEventData.put(key, URI.create(valueMap.getString("value")));
105114
} else {

ios/RNBatch.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ - (NSDictionary*) dictionaryWithEventDispatcherPayload:(id<BatchEventDispatcherP
357357
return;
358358
}
359359
[batchEventData putDouble:[(NSNumber*)value doubleValue] forKey:key];
360+
} else if ([@"date" isEqualToString:type]) {
361+
if (![value isKindOfClass:[NSNumber class]])
362+
{
363+
NSLog(@"RNBatch: Error while tracking event data: event data.attributes: expected number value, got something else");
364+
return;
365+
}
366+
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[(NSNumber*)value doubleValue] / 1000.0];
367+
[batchEventData putDate:date forKey:key];
360368
} else if ([@"url" isEqualToString:type]) {
361369
if (![value isKindOfClass:[NSString class]])
362370
{

src/BatchEventData.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ describe('BatchEventData', () => {
5050

5151
expect(spy).toHaveBeenCalled();
5252
});
53+
it(`handles a date attribute`, () => {
54+
const batchEventData = new BatchEventData();
55+
const spy = jest.spyOn(Logger, 'Log');
56+
57+
batchEventData.putDate('test_date', Date.now());
58+
59+
expect(spy).not.toHaveBeenCalled();
60+
});
5361
it(`handles an url attribute`, () => {
5462
const batchEventData = new BatchEventData();
5563
const spy = jest.spyOn(Logger, 'Log');

src/BatchEventData.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export enum TypedEventAttributeType {
1414
Boolean = 'boolean',
1515
Integer = 'integer',
1616
Float = 'float',
17+
Date = 'date',
1718
URL = 'url',
1819
}
1920

@@ -112,6 +113,22 @@ export class BatchEventData {
112113
return key.toLowerCase();
113114
}
114115

116+
public putDate(key: string, value: number): BatchEventData {
117+
key = this.prepareAttributeKey(key);
118+
try {
119+
this.checkBeforePuttingAttribute(key, value);
120+
} catch {
121+
return this;
122+
}
123+
124+
this._attributes[key] = {
125+
type: TypedEventAttributeType.Date,
126+
value,
127+
};
128+
129+
return this;
130+
}
131+
115132
public putURL(key: string, url: string): BatchEventData {
116133
key = this.prepareAttributeKey(key);
117134
try {

0 commit comments

Comments
 (0)