Skip to content

Commit

Permalink
feat: added missing methods (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 authored Feb 13, 2023
1 parent 8ccb5d2 commit 73f29e6
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy_git_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
"text": "Flutter SDK git tag created",
"username": "Flutter deployment bot",
"icon_url": "https://img.icons8.com/color/512/flutter.png",
"channel": "#squad-mobile",
"channel": "#mobile-deployments",
"blocks": [
{
"type": "section",
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
"text": "Flutter SDK deployment failure",
"username": "Flutter deployment bot",
"icon_url": "https://img.icons8.com/color/512/flutter.png",
"channel": "#squad-mobile",
"channel": "#mobile-deployments",
"blocks": [
{
"type": "section",
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/deploy_pubspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
"text": "Flutter SDK deployed to pub.dev",
"username": "Flutter deployment bot",
"icon_url": "https://img.icons8.com/color/512/flutter.png",
"channel": "#squad-mobile",
"channel": "#mobile-deployments",
"blocks": [
{
"type": "section",
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
"text": "Flutter SDK deployment failure",
"username": "Flutter deployment bot",
"icon_url": "https://img.icons8.com/color/512/flutter.png",
"channel": "#squad-mobile",
"channel": "#mobile-deployments",
"blocks": [
{
"type": "section",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/promote.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
"text": "Flutter SDK deployment failure",
"username": "Flutter deployment bot",
"icon_url": "https://img.icons8.com/color/512/flutter.png",
"channel": "#squad-mobile",
"channel": "#mobile-deployments",
"blocks": [
{
"type": "section",
Expand Down
8 changes: 4 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ android {
minSdkVersion 21
}

// instruct kotlin compiler to opt in to the APIs annotated with the kotlin.RequiresOptIn
// and io.customer.base.internal.InternalCustomerIOApi annotations
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
freeCompilerArgs += [
'-Xopt-in=kotlin.RequiresOptIn',
'-Xopt-in=io.customer.base.internal.InternalCustomerIOApi',
]
freeCompilerArgs += ['-Xopt-in=kotlin.RequiresOptIn',
'-Xopt-in=io.customer.base.internal.InternalCustomerIOApi',]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.CustomerIOShared
import io.customer.sdk.data.model.Region
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.extensions.getProperty
import io.customer.sdk.extensions.getString
import io.customer.sdk.extensions.takeIfNotBlank
Expand All @@ -26,6 +27,7 @@ import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import java.lang.ref.WeakReference

/**
* Android implementation of plugin that will let Flutter developers to
Expand All @@ -38,13 +40,13 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
/// when the Flutter Engine is detached from the Activity
private lateinit var flutterCommunicationChannel: MethodChannel
private lateinit var context: Context
private var activity: Activity? = null
private var activity: WeakReference<Activity>? = null

private val logger: Logger
get() = CustomerIOShared.instance().diStaticGraph.logger

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
this.activity = binding.activity
this.activity = WeakReference(binding.activity)
}

override fun onDetachedFromActivityForConfigChanges() {
Expand Down Expand Up @@ -100,6 +102,16 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
track(it)
}
}
Keys.Methods.TRACK_METRIC -> {
call.toNativeMethodCall(result) {
trackMetric(it)
}
}
Keys.Methods.REGISTER_DEVICE_TOKEN -> {
call.toNativeMethodCall(result) {
registerDeviceToken(it)
}
}
Keys.Methods.SET_DEVICE_ATTRIBUTES -> {
call.toNativeMethodCall(result) {
setDeviceAttributes(it)
Expand Down Expand Up @@ -142,9 +154,29 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
}
}

private fun registerDeviceToken(params: Map<String, Any>) {
val token = params.getString(Keys.Tracking.TOKEN)
CustomerIO.instance().registerDeviceToken(token)
}

private fun trackMetric(params: Map<String, Any>) {
val deliveryId = params.getString(Keys.Tracking.DELIVERY_ID)
val deliveryToken = params.getString(Keys.Tracking.DELIVERY_TOKEN)
val eventName = params.getProperty<String>(Keys.Tracking.METRIC_EVENT)
val event = MetricEvent.getEvent(eventName)

if (event == null) {
logger.info("metric event type null. Possible issue with SDK? Given: $eventName")
return
}

CustomerIO.instance().trackMetric(
deliveryID = deliveryId, deviceToken = deliveryToken, event = event
)
}

private fun setDeviceAttributes(params: Map<String, Any>) {
val attributes =
params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: emptyMap()
val attributes = params.getProperty<Map<String, Any>>(Keys.Tracking.ATTRIBUTES) ?: return

CustomerIO.instance().deviceAttributes = attributes
}
Expand Down Expand Up @@ -191,7 +223,7 @@ class CustomerIoPlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
module = ModuleMessagingInApp(
config = MessagingInAppModuleConfig.Builder()
.setEventListener(CustomerIOInAppEventListener { method, args ->
this@CustomerIoPlugin.activity?.runOnUiThread {
this@CustomerIoPlugin.activity?.get()?.runOnUiThread {
flutterCommunicationChannel.invokeMethod(method, args)
}
}).build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ internal object Keys {
const val SCREEN = "screen"
const val SET_DEVICE_ATTRIBUTES = "setDeviceAttributes"
const val SET_PROFILE_ATTRIBUTES = "setProfileAttributes"
const val REGISTER_DEVICE_TOKEN = "registerDeviceToken"
const val TRACK_METRIC = "trackMetric"
}

object Tracking {
const val IDENTIFIER = "identifier"
const val ATTRIBUTES = "attributes"
const val EVENT_NAME = "eventName"
const val TOKEN = "token"
const val DELIVERY_ID = "deliveryId"
const val DELIVERY_TOKEN = "deliveryToken"
const val METRIC_EVENT = "metricEvent"
}

object Environment {
Expand All @@ -24,5 +30,4 @@ internal object Keys {
const val REGION = "region"
const val ENABLE_IN_APP = "enableInApp"
}

}
42 changes: 34 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:customer_io/customer_io.dart';
import 'package:customer_io/customer_io_config.dart';
import 'package:customer_io/customer_io_enums.dart';
import 'package:customer_io/customer_io_inapp.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand All @@ -12,11 +13,14 @@ void main() async {

await dotenv.load(fileName: "credentials.env");
await CustomerIO.initialize(
config: CustomerIOConfig(
siteId: dotenv.get('siteId', fallback: 'YOUR_SITE_ID'),
apiKey: dotenv.get('apiKey', fallback: 'YOUR_API_KEY'),
enableInApp: true));

config: CustomerIOConfig(
siteId: dotenv.get('siteId', fallback: 'YOUR_SITE_ID'),
apiKey: dotenv.get('apiKey', fallback: 'YOUR_API_KEY'),
logLevel: CioLogLevel.debug,
enableInApp: true,
autoTrackDeviceAttributes: true,
autoTrackPushEvents: true),
);
runApp(const MyApp());
}

Expand All @@ -41,8 +45,8 @@ class _MyAppState extends State<MyApp> {
void initState() {
super.initState();
CustomerIO.identify(
identifier: "fel-ios",
attributes: {"email": "[email protected]"});
identifier: "identifier",
attributes: {"email": "email"});
}

@override
Expand Down Expand Up @@ -99,12 +103,34 @@ class _MyAppState extends State<MyApp> {
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('REGISTER DEVICE'),
onPressed: () {
CustomerIO.registerDeviceToken(deviceToken: "device-token");
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('TRACK METRIC'),
onPressed: () {
CustomerIO.trackMetric(
deliveryID: "deliveryID101",
deviceToken: "deviceToken2011",
event: MetricEvent.clicked);
},
),
),
const Spacer(),
Center(
child: ElevatedButton(
child: const Text('SUBSCRIBE IN-APP MESSAGE EVENTS'),
onPressed: () {
inAppMessageStreamSubscription =
CustomerIO.subscribeToInAppEventListener((InAppEvent event) {
CustomerIO.subscribeToInAppEventListener(
(InAppEvent event) {
if (kDebugMode) {
print("Received event: ${event.eventType.name}");
}
Expand Down
1 change: 0 additions & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ dependencies:
sdk: flutter
flutter_dotenv: ^5.0.2


customer_io:
# When depending on this package from a real application you should use:
# customer_io: ^x.y.z
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ struct Keys {
static let screen = "screen"
static let setDeviceAttributes = "setDeviceAttributes"
static let setProfileAttributes = "setProfileAttributes"
static let registerDeviceToken = "registerDeviceToken"
static let trackMetric = "trackMetric"

}

struct Tracking {
static let identifier = "identifier"
static let attributes = "attributes"
static let eventName = "eventName"
static let token = "token"
static let deliveryId = "deliveryId"
static let deliveryToken = "deliveryToken"
static let metricEvent = "metricEvent"
}

struct Environment{
Expand Down
30 changes: 30 additions & 0 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
call.toNativeMethodCall(result: result) {
setDeviceAttributes(params: $0)
}
case Keys.Methods.registerDeviceToken:
call.toNativeMethodCall(result: result) {
registerDeviceToken(params: $0)
}
case Keys.Methods.trackMetric:
call.toNativeMethodCall(result: result) {
trackMetric(params: $0)
}
default:
result(FlutterMethodNotImplemented)
}
Expand Down Expand Up @@ -119,6 +127,28 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
CustomerIO.shared.profileAttributes = attributes
}

private func registerDeviceToken(params : Dictionary<String, AnyHashable>){
guard let token = params[Keys.Tracking.token] as? String
else {
return
}

CustomerIO.shared.registerDeviceToken(token)
}

private func trackMetric(params : Dictionary<String, AnyHashable>){
guard let deliveryId = params[Keys.Tracking.deliveryId] as? String,
let deviceToken = params[Keys.Tracking.deliveryToken] as? String,
let metricEvent = params[Keys.Tracking.metricEvent] as? String,
let event = Metric.getEvent(from: metricEvent)
else {
return
}

CustomerIO.shared.trackMetric(deliveryID: deliveryId,
event: event,
deviceToken: deviceToken)
}

private func initialize(params : Dictionary<String, AnyHashable>){
guard let siteId = params[Keys.Environment.siteId] as? String,
Expand Down
6 changes: 3 additions & 3 deletions ios/customer_io.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
Pod::Spec.new do |s|
s.name = 'customer_io'
s.version = '1.0.0-alpha.5'
s.version = '1.0.0-alpha.8'
s.summary = 'Customer.io plugin for Flutter'
s.homepage = 'https://customer.io/'
s.license = { :file => '../LICENSE' }
Expand All @@ -13,8 +13,8 @@ Pod::Spec.new do |s|
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '13.0'
s.dependency "CustomerIOTracking", '~> 2.1.0-beta.1'
s.dependency "CustomerIOMessagingInApp", '~> 2.1.0-beta.1'
s.dependency "CustomerIOTracking", '~> 2.1.0-beta.2'
s.dependency "CustomerIOMessagingInApp", '~> 2.1.0-beta.2'

# Flutter.framework does not contain a i386 slice.
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
Expand Down
16 changes: 16 additions & 0 deletions lib/customer_io.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';

import 'customer_io_config.dart';
import 'customer_io_enums.dart';
import 'customer_io_inapp.dart';
import 'customer_io_platform_interface.dart';

Expand Down Expand Up @@ -49,6 +50,21 @@ class CustomerIO {
return _customerIO.track(name: name, attributes: attributes);
}

/// Track a push metric
static void trackMetric(
{required String deliveryID,
required String deviceToken,
required MetricEvent event}) {
return _customerIO.trackMetric(
deliveryID: deliveryID, deviceToken: deviceToken, event: event);
}

/// Register a new device token with Customer.io, associated with the current active customer. If there
/// is no active customer, this will fail to register the device
static void registerDeviceToken({required String deviceToken}) {
return _customerIO.registerDeviceToken(deviceToken: deviceToken);
}

/// Track screen events to record the screens a user visits
///
/// @param name name of the screen user visited
Expand Down
6 changes: 6 additions & 0 deletions lib/customer_io_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ class MethodConsts {
static const String identify = "identify";
static const String clearIdentify = "clearIdentify";
static const String track = "track";
static const String trackMetric = "trackMetric";
static const String screen = "screen";
static const String setDeviceAttributes = "setDeviceAttributes";
static const String setProfileAttributes = "setProfileAttributes";
static const String registerDeviceToken = "registerDeviceToken";
}

class TrackingConsts {
static const String identifier = "identifier";
static const String attributes = "attributes";
static const String eventName = "eventName";
static const String token = "token";
static const String deliveryId = "deliveryId";
static const String deliveryToken = "deliveryToken";
static const String metricEvent = "metricEvent";
}
Loading

0 comments on commit 73f29e6

Please sign in to comment.