Skip to content

Commit a951a36

Browse files
Custom metrics feature added (#58)
* Custom Metric override feature added. * SDK version updated to 20.11.7 * Index issue fixed * Removed metrics enum * Safe checks added * Example added for custom metrics * Removed extra data from custom metrics example * — Added checks in 'setCustomMetrics' function for data validation of metrics passed by user. — Updated documentation for 'setCustomMetrics' function * Update setCustomMetrics function to async for await usage * — Warning text updated — length check added — changed if to else if * Changing metric comment * Rework of if else statements and added return statements Co-authored-by: ArtursKadikis <[email protected]>
1 parent e16afa1 commit a951a36

File tree

9 files changed

+81
-5
lines changed

9 files changed

+81
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 20.11.7
2+
* Added "setCustomMetrics" method to set the metrics you want to override
3+
14
## 20.11.6
25
* !! To handle the push notification you need to add the notifcation callback "didReceiveNotificationResponse" in your "AppDelegate.m" file and send the reponse to Countly SDK using this function "onNotificationResponse", for more detials please check the below mentioned link of "Handling push callbacks" section in Countly SDK documentation.
36
https://support.count.ly/hc/en-us/articles/360037813231-React-Native-Bridge-#handling-push-callbacks

Countly.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,43 @@ Countly.appLoadingFinished = async function(){
844844
CountlyReactNative.appLoadingFinished()
845845
}
846846

847+
/**
848+
* Set the metrics you want to override
849+
* Should be called before Countly init
850+
* @param {Object} customMetric - metric with key/value pair
851+
* Supported data type for customMetric values is String
852+
*/
853+
Countly.setCustomMetrics = async function(customMetric){
854+
if(!customMetric) {
855+
if(await CountlyReactNative.isLoggingEnabled()) {
856+
console.error("[CountlyReactNative] setCustomMetrics, customMetric should not be null or undefined");
857+
}
858+
return "customMetric should not be null or undefined";
859+
}
860+
if(typeof customMetric !== 'object'){
861+
if(await CountlyReactNative.isLoggingEnabled()) {
862+
console.warn("[CountlyReactNative] setCustomMetrics, unsupported data type of customMetric '" + (typeof customMetric) + "'");
863+
}
864+
return "Unsupported data type of customMetric '" + (typeof customMetric) + "'";
865+
}
866+
var args = [];
867+
customMetric = customMetric || {};
868+
for(var key in customMetric){
869+
if (typeof customMetric[key] == "string") {
870+
args.push(key.toString());
871+
args.push(customMetric[key].toString());
872+
}
873+
else {
874+
if(await CountlyReactNative.isLoggingEnabled()) {
875+
console.warn("[CountlyReactNative] setCustomMetrics, skipping value for key '" + key.toString() + "', due to unsupported data type '" + (typeof customMetric[key]) + "'");
876+
}
877+
}
878+
}
879+
if(args.length != 0) {
880+
CountlyReactNative.setCustomMetrics(args);
881+
}
882+
}
883+
847884
/*
848885
Countly.initNative = function(){
849886
CountlyReactNative.initNative();

CountlyReactNative.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'CountlyReactNative'
3-
s.version = '20.11.6'
3+
s.version = '20.11.7'
44
s.license = {
55
:type => 'COMMUNITY',
66
:text => <<-LICENSE

android/src/main/java/ly/count/android/sdk/react/CountlyReactNative.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public String toString(){
7474
public class CountlyReactNative extends ReactContextBaseJavaModule implements LifecycleEventListener {
7575

7676
public static final String TAG = "CountlyRNPlugin";
77-
private String COUNTLY_RN_SDK_VERSION_STRING = "20.11.6";
77+
private String COUNTLY_RN_SDK_VERSION_STRING = "20.11.7";
7878
private String COUNTLY_RN_SDK_NAME = "js-rnb-android";
7979

8080
private static CountlyConfig config = new CountlyConfig();
@@ -1015,6 +1015,21 @@ public void recordAttributionID(ReadableArray args){
10151015
public void appLoadingFinished(){
10161016
Countly.sharedInstance().apm().setAppIsLoaded();
10171017
}
1018+
1019+
@ReactMethod
1020+
public void setCustomMetrics(ReadableArray args){
1021+
Map<String, String> customMetric = new HashMap<>();
1022+
for (int i = 0, il = args.size(); i < il; i += 2) {
1023+
try{
1024+
if(i+1 < il) {
1025+
customMetric.put(args.getString(i), args.getString(i + 1));
1026+
}
1027+
}catch(Exception exception){
1028+
log("setCustomMetrics, could not parse metrics, skipping it. ", LogLevel.ERROR);
1029+
}
1030+
}
1031+
this.config.setMetricOverride(customMetric);
1032+
}
10181033

10191034
enum LogLevel {INFO, DEBUG, VERBOSE, WARNING, ERROR}
10201035
static void log(String message, LogLevel logLevel) {

example/Example.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,13 @@ class Example extends Component {
431431
Countly.testCrash();
432432
}
433433
*/
434+
setCustomMetrics(){
435+
var customMetric = {
436+
"_carrier": "Custom Carrier"
437+
};
438+
Countly.setCustomMetrics(customMetric);
439+
}
440+
434441

435442
test(){
436443
this.onInit();
@@ -534,6 +541,7 @@ class Example extends Component {
534541
< Button onPress = { this.showNPS } title = "Show NPS" color = "#00b5ad"> </Button>
535542
< Button onPress = { this.eventSendThreshold } title = "Set Event Threshold" color = "#00b5ad"> </Button>
536543
< Button onPress = { this.setCustomCrashSegments } title = "Set Custom Crash Segment" color = "#00b5ad"> </Button>
544+
< Button onPress = { this.setCustomMetrics } title = "Set Custom Metrics" color = "#00b5ad"> </Button>
537545
<Text style={[{textAlign: 'center'}]}>Other Methods End</Text>
538546
<Text style={[{textAlign: 'center'}]}>.</Text>
539547

example/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rm App.js
1111
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/App.js --output App.js
1212
curl https://raw.githubusercontent.com/Countly/countly-sdk-react-native-bridge/master/example/Example.js --output Example.js
1313

14-
14+
1515

1616
cd ./ios
1717
pod install

ios/src/CountlyReactNative.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ typedef void (^Result)(id _Nullable result);
6464
- (void)endTrace:(NSArray*_Nullable)arguments;
6565
- (void)recordNetworkTrace:(NSArray*_Nullable)arguments;
6666
- (void)enableApm:(NSArray*_Nullable)arguments;
67+
- (void)setCustomMetrics:(NSArray*_Nullable)arguments;
6768

6869
- (void)enableAttribution;
6970
- (void)recordAttributionID:(NSArray*_Nullable)arguments;

ios/src/CountlyReactNative.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ @interface CountlyFeedbackWidget ()
2121
+ (CountlyFeedbackWidget *)createWithDictionary:(NSDictionary *)dictionary;
2222
@end
2323

24-
NSString* const kCountlyReactNativeSDKVersion = @"20.11.6";
24+
NSString* const kCountlyReactNativeSDKVersion = @"20.11.7";
2525
NSString* const kCountlyReactNativeSDKName = @"js-rnb-ios";
2626

2727
CountlyConfig* config = nil;
@@ -1056,6 +1056,18 @@ + (void) log: (NSString *) theMessage{
10561056
});
10571057
}
10581058

1059+
RCT_EXPORT_METHOD(setCustomMetrics:(NSArray*)arguments) {
1060+
dispatch_async(dispatch_get_main_queue(), ^ {
1061+
NSMutableDictionary *metrics = [[NSMutableDictionary alloc] init];
1062+
for(int i=0,il=(int)arguments.count;i<il;i+=2){
1063+
if(i+1 < il){
1064+
metrics[[arguments objectAtIndex:i]] = [arguments objectAtIndex:i+1];
1065+
}
1066+
}
1067+
config.customMetrics = metrics;
1068+
});
1069+
}
1070+
10591071
- (void)addCountlyFeature:(CLYFeature)feature
10601072
{
10611073
if(countlyFeatures == nil) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "countly-sdk-react-native-bridge",
3-
"version": "20.11.6",
3+
"version": "20.11.7",
44
"author": "Countly <[email protected]> (https://count.ly/)",
55
"bugs": {
66
"url": "https://github.com/Countly/countly-sdk-react-native-bridge/issues"

0 commit comments

Comments
 (0)