@screeb/react-native 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+ Copyright (c) 2021 Screeb
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # @screeb/react-native
2
+
3
+ A react-native module to integrate Screeb mobile sdk for Android and/or iOS.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ npm install @screeb/react-native
9
+ ```
10
+
11
+ ## iOS specific configuration
12
+ You should set IOS target build configuration `BUILD_LIBRARY_FOR_DISTRIBUTION` to `YES` in your `Podfile` to avoid runtime crash:
13
+ ```ruby
14
+ post_install do |installer|
15
+ ...
16
+ installer.pods_project.targets.each do |target|
17
+ ...
18
+
19
+ target.build_configurations.each do |config|
20
+ ...
21
+ config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
22
+ end
23
+ end
24
+ end
25
+ ```
26
+
27
+ ## Android specific configuration
28
+
29
+ First, you should use MultidexApplication if not yet to avoid compilation issues.
30
+
31
+ ```
32
+ defaultConfig {
33
+ (...)
34
+ multiDexEnabled true
35
+ }
36
+ (...)
37
+ dependencies {
38
+ (...)
39
+ implementation 'androidx.multidex:multidex:2.0.1'
40
+ }
41
+ ```
42
+
43
+ Then, the Android sdk needs to be notified of activities lifecycle changes to be correctly started.
44
+
45
+ It is mandatory to pass the Application context to the module in your custom Application class
46
+ in the `onCreate` function :
47
+
48
+ ```kotlin
49
+ override fun onCreate() {
50
+ super.onCreate()
51
+ ScreebModuleModule.setAppContext(this)
52
+ }
53
+ ```
54
+
55
+ ## Usage
56
+
57
+ ```js
58
+ import { initSdk, trackScreen, trackEvent, setProperties, setIdentity } from "@screeb/react-native";
59
+
60
+ // Init the sdk at app start (useEffect hook used here, but componentDidMount is fine)
61
+ React.useEffect(() => {
62
+ initSdk(
63
+ "<android-channel-id>",
64
+ "<ios-channel-id>",
65
+ "<user-identity>",
66
+ {
67
+ 'example-prop1': false,
68
+ 'example-prop2': 29,
69
+ 'example-prop3' : 'iPhone 13',
70
+ }
71
+ );
72
+ }, []);
73
+
74
+ (...)
75
+
76
+ // SetIdentity command :
77
+ setIdentity(
78
+ '<user-identity>',
79
+ {
80
+ 'example-prop1': false,
81
+ 'example-prop2': 29,
82
+ 'example-prop3' : 'iPhone 13',
83
+ }
84
+ );
85
+
86
+ (...)
87
+
88
+ // trackEvent command :
89
+ trackEvent(
90
+ '<event-name>',
91
+ {
92
+ 'example-prop1': false,
93
+ 'example-prop2': 29,
94
+ 'example-prop3' : 'iPhone 13',
95
+ }
96
+ );
97
+
98
+ (...)
99
+
100
+ // trackScreen command :
101
+ trackScreen(
102
+ '<screen-name>',
103
+ {
104
+ 'example-prop1': false,
105
+ 'example-prop2': 29,
106
+ 'example-prop3' : 'iPhone 13',
107
+ }
108
+ );
109
+
110
+ (...)
111
+
112
+ // setProperties command :
113
+ setProperties(
114
+ {
115
+ 'example-prop1': false,
116
+ 'example-prop2': 29,
117
+ 'example-prop3' : 'iPhone 13',
118
+ }
119
+ );
120
+ ```
121
+
122
+ ## License
123
+
124
+ MIT
@@ -0,0 +1,129 @@
1
+ buildscript {
2
+ // Buildscript is evaluated before everything else so we can't use getExtOrDefault
3
+ def kotlin_version = rootProject.ext.has('kotlinVersion') ? rootProject.ext.get('kotlinVersion') : project.properties['ScreebModule_kotlinVersion']
4
+
5
+ repositories {
6
+ google()
7
+ mavenCentral()
8
+ }
9
+
10
+ dependencies {
11
+ classpath 'com.android.tools.build:gradle:3.2.1'
12
+ // noinspection DifferentKotlinGradleVersion
13
+ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14
+ }
15
+ }
16
+
17
+ apply plugin: 'com.android.library'
18
+ apply plugin: 'kotlin-android'
19
+
20
+ def getExtOrDefault(name) {
21
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['ScreebModule_' + name]
22
+ }
23
+
24
+ def getExtOrIntegerDefault(name) {
25
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['ScreebModule_' + name]).toInteger()
26
+ }
27
+
28
+ android {
29
+ compileSdkVersion getExtOrIntegerDefault('compileSdkVersion')
30
+ defaultConfig {
31
+ minSdkVersion 16
32
+ targetSdkVersion getExtOrIntegerDefault('targetSdkVersion')
33
+ versionCode 1
34
+ versionName "0.2.0"
35
+
36
+ }
37
+
38
+ buildTypes {
39
+ release {
40
+ minifyEnabled false
41
+ }
42
+ }
43
+ lintOptions {
44
+ disable 'GradleCompatible'
45
+ }
46
+ compileOptions {
47
+ sourceCompatibility JavaVersion.VERSION_1_8
48
+ targetCompatibility JavaVersion.VERSION_1_8
49
+ }
50
+ }
51
+
52
+ repositories {
53
+ mavenCentral()
54
+ google()
55
+
56
+ def found = false
57
+ def defaultDir = null
58
+ def androidSourcesName = 'React Native sources'
59
+
60
+ if (rootProject.ext.has('reactNativeAndroidRoot')) {
61
+ defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
62
+ } else {
63
+ defaultDir = new File(
64
+ projectDir,
65
+ '/../../../node_modules/react-native/android'
66
+ )
67
+ }
68
+
69
+ if (defaultDir.exists()) {
70
+ maven {
71
+ url defaultDir.toString()
72
+ name androidSourcesName
73
+ }
74
+
75
+ logger.info(":${project.name}:reactNativeAndroidRoot ${defaultDir.canonicalPath}")
76
+ found = true
77
+ } else {
78
+ def parentDir = rootProject.projectDir
79
+
80
+ 1.upto(5, {
81
+ if (found) return true
82
+ parentDir = parentDir.parentFile
83
+
84
+ def androidSourcesDir = new File(
85
+ parentDir,
86
+ 'node_modules/react-native'
87
+ )
88
+
89
+ def androidPrebuiltBinaryDir = new File(
90
+ parentDir,
91
+ 'node_modules/react-native/android'
92
+ )
93
+
94
+ if (androidPrebuiltBinaryDir.exists()) {
95
+ maven {
96
+ url androidPrebuiltBinaryDir.toString()
97
+ name androidSourcesName
98
+ }
99
+
100
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidPrebuiltBinaryDir.canonicalPath}")
101
+ found = true
102
+ } else if (androidSourcesDir.exists()) {
103
+ maven {
104
+ url androidSourcesDir.toString()
105
+ name androidSourcesName
106
+ }
107
+
108
+ logger.info(":${project.name}:reactNativeAndroidRoot ${androidSourcesDir.canonicalPath}")
109
+ found = true
110
+ }
111
+ })
112
+ }
113
+
114
+ if (!found) {
115
+ throw new GradleException(
116
+ "${project.name}: unable to locate React Native android sources. " +
117
+ "Ensure you have you installed React Native as a dependency in your project and try again."
118
+ )
119
+ }
120
+ }
121
+
122
+ def kotlin_version = getExtOrDefault('kotlinVersion')
123
+
124
+ dependencies {
125
+ // noinspection GradleDynamicVersion
126
+ api 'com.facebook.react:react-native:+'
127
+ implementation "app.screeb.sdk:survey:1.6.0"
128
+ implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.10"
129
+ }
@@ -0,0 +1,3 @@
1
+ ScreebModule_kotlinVersion=1.6.0
2
+ ScreebModule_compileSdkVersion=29
3
+ ScreebModule_targetSdkVersion=29
@@ -0,0 +1,4 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.screebmodule">
3
+
4
+ </manifest>
@@ -0,0 +1,82 @@
1
+ package com.screebmodule
2
+
3
+ import com.facebook.react.bridge.ReactApplicationContext
4
+ import com.facebook.react.bridge.ReactContextBaseJavaModule
5
+ import com.facebook.react.bridge.ReactMethod
6
+ import com.facebook.react.bridge.Promise
7
+ import com.facebook.react.bridge.ReadableMap
8
+ import java.util.HashMap
9
+ import androidx.annotation.NonNull
10
+ import android.content.Context
11
+ import android.util.Log
12
+ import app.screeb.sdk.Screeb
13
+
14
+ class ScreebModuleModule(reactContext: ReactApplicationContext) :
15
+ ReactContextBaseJavaModule(reactContext) {
16
+
17
+ override fun getName(): String {
18
+ return "ScreebModule"
19
+ }
20
+
21
+ @ReactMethod
22
+ fun initSdk(channelId: String, userId: String?, properties: ReadableMap?) {
23
+ Log.d("ScreebModule", "Called setIdentity : $userId")
24
+ var map: HashMap<String, Any?>? = null
25
+ if (properties != null) {
26
+ map = properties.toHashMap()
27
+ }
28
+ screeb?.pluginInit(channelId, userId, map)
29
+ }
30
+
31
+ @ReactMethod
32
+ fun setIdentity(userId: String, properties: ReadableMap?) {
33
+ Log.d("ScreebModule", "Called setIdentity : $userId")
34
+ var map: HashMap<String, Any?>? = null
35
+ if (properties != null) {
36
+ map = properties.toHashMap()
37
+ }
38
+ screeb?.setIdentity(userId, map)
39
+ }
40
+
41
+ @ReactMethod
42
+ fun trackEvent(eventId: String, properties: ReadableMap?) {
43
+ Log.d("ScreebModule", "Called trackEvent : $eventId")
44
+ var map: HashMap<String, Any?>? = null
45
+ if (properties != null) {
46
+ map = properties.toHashMap()
47
+ }
48
+ screeb?.trackEvent(eventId, map)
49
+ }
50
+
51
+ @ReactMethod
52
+ fun trackScreen(screen: String, properties: ReadableMap?) {
53
+ Log.d("ScreebModule", "Called trackScreen : $screen")
54
+ var map: HashMap<String, Any?>? = null
55
+ if (properties != null) {
56
+ map = properties.toHashMap()
57
+ }
58
+ screeb?.trackScreen(screen, map)
59
+ }
60
+
61
+ @ReactMethod
62
+ fun setProperties(properties: ReadableMap) {
63
+ Log.d(
64
+ "ScreebModule",
65
+ "Called setVisitorProperties with " + properties.toHashMap().size + " properties"
66
+ )
67
+ screeb?.setVisitorProperties(properties.toHashMap())
68
+ }
69
+
70
+ companion object {
71
+ var screeb: Screeb? = null
72
+
73
+ @JvmStatic
74
+ fun setAppContext(context: Context){
75
+ screeb = Screeb.Builder()
76
+ .withContext(context)
77
+ .withPluginMode(true)
78
+ .build()
79
+ }
80
+
81
+ }
82
+ }
@@ -0,0 +1,17 @@
1
+ package com.screebmodule
2
+
3
+ import com.facebook.react.ReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.uimanager.ViewManager
7
+
8
+
9
+ class ScreebModulePackage : ReactPackage {
10
+ override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
11
+ return listOf(ScreebModuleModule(reactContext))
12
+ }
13
+
14
+ override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
15
+ return emptyList()
16
+ }
17
+ }
@@ -0,0 +1,2 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTViewManager.h>
@@ -0,0 +1,14 @@
1
+ #import <React/RCTBridgeModule.h>
2
+
3
+ @interface RCT_EXTERN_MODULE(ScreebModule, NSObject)
4
+
5
+ RCT_EXTERN_METHOD(initSdk:(NSString *)channelId userId:(NSString *)userId_ properties:(NSDictionary<NSString *, id> *)properties_)
6
+ RCT_EXTERN_METHOD(setIdentity:(NSString *)userId properties:(NSDictionary<NSString *, id> *)properties_)
7
+ RCT_EXTERN_METHOD(trackEvent:(NSString *)eventId properties:(NSDictionary<NSString *, id> *)properties_)
8
+ RCT_EXTERN_METHOD(trackScreen:(NSString *)screen properties:(NSDictionary<NSString *, id> *)properties_)
9
+ RCT_EXTERN_METHOD(setProperties:(NSDictionary<NSString *, id> *)properties)
10
+ + (BOOL)requiresMainQueueSetup
11
+ {
12
+ return YES;
13
+ }
14
+ @end
@@ -0,0 +1,70 @@
1
+ import Screeb
2
+ import UIKit
3
+
4
+ @objc(ScreebModule)
5
+ class ScreebModule: NSObject {
6
+
7
+ @objc(initSdk:userId:properties:)
8
+ func initSdk(
9
+ _ channelId: String,
10
+ userId userId_: String?,
11
+ properties properties_: [String: Any]?) {
12
+ var map: [String: AnyEncodable?] = [:]
13
+ if (properties_ != nil) {
14
+ map = self.mapToAnyEncodable(map: properties_!)
15
+ }
16
+ if let controller = UIApplication.shared.keyWindow?.rootViewController {
17
+ Screeb.initSdk(context: controller, channelId: channelId, identity: userId_, visitorProperty: map)
18
+ } else {
19
+ print("Screeb : error init, could not find rootViewController")
20
+ }
21
+ }
22
+
23
+ @objc func setIdentity(_ userId: String, properties properties_: [String: Any]?) {
24
+ var map: [String: AnyEncodable?] = [:]
25
+ if (properties_ != nil) {
26
+ map = self.mapToAnyEncodable(map: properties_!)
27
+ }
28
+ Screeb.setIdentity(uniqueVisitorId: userId, visitorProperty: map)
29
+ }
30
+
31
+ @objc func trackEvent(_ eventId: String, properties properties_: [String: Any]?) {
32
+ var map: [String: AnyEncodable?] = [:]
33
+ if (properties_ != nil) {
34
+ map = self.mapToAnyEncodable(map: properties_!)
35
+ }
36
+ Screeb.trackEvent(name: eventId, trackingEventProperties: map)
37
+ }
38
+
39
+ @objc func trackScreen(_ screen: String, properties properties_: [String: Any]?) {
40
+ var map: [String: AnyEncodable?] = [:]
41
+ if (properties_ != nil) {
42
+ map = self.mapToAnyEncodable(map: properties_!)
43
+ }
44
+ Screeb.trackScreen(name: screen, trackingEventProperties: map)
45
+ }
46
+
47
+ @objc(setProperties:)
48
+ func setVisitorPropertiesImpl(_ properties: [String: Any]) {
49
+ let map = self.mapToAnyEncodable(map: properties)
50
+ Screeb.visitorProperty(visitorProperty: map)
51
+ }
52
+
53
+ private func mapToAnyEncodable(map: [String: Any]) -> [String: AnyEncodable?] {
54
+ return map.mapValues{
55
+ value in
56
+ switch value {
57
+ case is String:
58
+ return AnyEncodable(value as! String)
59
+ case is Bool:
60
+ return AnyEncodable(value as! Bool)
61
+ case is Float:
62
+ return AnyEncodable(value as! Float)
63
+ case is Int:
64
+ return AnyEncodable(value as! Int)
65
+ default: break
66
+ }
67
+ return nil
68
+ }
69
+ }
70
+ }