Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array Support #422

Open
wants to merge 5 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Countly.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
type SegmentationValue = number | string | boolean | SegmentationValue[] | { [key: string]: SegmentationValue };

interface Segmentation {
[key: string]: number | string | boolean;
[key: string]: SegmentationValue;
}

interface CountlyEventOptions {
Expand Down
9 changes: 2 additions & 7 deletions Validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,28 +134,23 @@ function areEventParametersValid(functionName, eventName, segmentation, eventCou
return false;
}

// validate segmentation values
if (segmentation) {
for (const key in segmentation) {
const value = segmentation[key];
const valueType = typeof value;
if (value && valueType !== "string" && valueType !== "number" && valueType !== "boolean") {
L.w(`${functionName}, segmentation value: [${value}] for the key: [${key}] must be a number, string or boolean!`);
if (!value) {
L.w(`${functionName}, provided value for the key: [${key}] is null!`);
return false;
}
}
}

if (eventCount && (typeof eventCount !== "number" || eventCount < 0)) {
L.w(`${functionName}, provided eventCount: [${eventCount}]. It must be a positive number!`);
return false;
}

if (eventSum && typeof eventSum !== "number") {
L.w(`${functionName}, provided eventSum: [${eventSum}]. It must be a number!`);
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,30 @@ public Map<String, Object> convertToEventMap(ReadableArray segments) {
segmentation.put(key, doubleValue);
}
break;
case Array:
ReadableArray arrayValue = segments.getArray(i + 1);
AliRKat marked this conversation as resolved.
Show resolved Hide resolved
List<Object> arrayList = new ArrayList<>();
for (int j = 0; j < arrayValue.size(); j++) {
ReadableType elementType = arrayValue.getType(j);
switch (elementType) {
case String:
arrayList.add(arrayValue.getString(j));
break;
case Boolean:
arrayList.add(arrayValue.getBoolean(j));
break;
case Number:
double elemDouble = arrayValue.getDouble(j);
int elemInt = (int) elemDouble;
arrayList.add(elemDouble == elemInt ? elemInt : elemDouble);
break;
default:
// Skip non-primitive types
break;
}
}
segmentation.put(key, arrayList);
break;
default:
// Skip other types
break;
Expand Down
25 changes: 22 additions & 3 deletions example/CountlyRNExample/Events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,28 @@ const eventWithSum = () => {
Countly.events.recordEvent("Event With Sum", undefined, 1, 0.99);
};
const eventWithSegment = () => {
// example for event with segment
Countly.events.recordEvent("Event With Segment", { Country: "Paris", Age: 28 }, 1, undefined);
Countly.events.recordEvent("Event With Segment", { Country: "France", Age: 38 }, 1, undefined);
const segment: Segmentation = {
stringList: ['value1', 'value2', 'value3'],
intList: [1, 2, 3],
doubleList: [1.1, 2.2, 3.3],
boolList: [true, false, true],
mixedList: ['value1', 2, 3.3, true],
mapList: [ // currently this is not supported
{ key1: 'value1', key2: 2 },
{ key1: 'value2', key2: 3 },
{ key1: 'value3', key2: 4 }
],
nestedList: [ // currently this is not supported
['value1', 'value2'],
['value3', 'value4'],
['value5', 'value6']
],
normalString: 'normalString',
normalInt: 1,
normalDouble: 1.1,
normalBool: true
};
Countly.events.recordEvent("Event With Segment With Mixed Types", segment, 1, undefined);
};
const eventWithSumAndSegment = () => {
// example for event with segment and sum
Expand Down