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

added setTrackingOptions #143

Open
wants to merge 1 commit into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import android.content.Context
import com.amplitude.api.Amplitude
import com.amplitude.api.AmplitudeServerZone
import com.amplitude.api.TrackingOptions
import com.amplitude.api.Identify
import com.amplitude.api.Revenue
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -14,6 +15,7 @@ import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import org.json.JSONArray
import org.json.JSONObject
import android.util.Log

class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler {
companion object {
Expand Down Expand Up @@ -42,7 +44,7 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler {
override fun onMethodCall(call: MethodCall, result: Result) {
val json = JSONObject(call.arguments.toString())
val instanceName = json["instanceName"].toString()

when (call.method) {
"init" -> {
val client = Amplitude.getInstance(instanceName)
Expand Down Expand Up @@ -249,6 +251,68 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler {

result.success("setServerZone called..")
}

"setTrackingOptions" -> {
val client = Amplitude.getInstance(instanceName)
val options = TrackingOptions()

if(!json.getBoolean("Adid"))
options.disableAdid()

if(!json.getBoolean("ApiLevel"))
options.disableApiLevel()

if(!json.getBoolean("AppSetId"))
options.disableAppSetId()

if(!json.getBoolean("Carrier"))
options.disableCarrier()

if(!json.getBoolean("City"))
options.disableCity()

if(!json.getBoolean("Country"))
options.disableCountry()

if(!json.getBoolean("DeviceBrand"))
options.disableDeviceBrand()

if(!json.getBoolean("DeviceManufacturer"))
options.disableDeviceManufacturer()

if(!json.getBoolean("DeviceModel"))
options.disableDeviceModel()

if(!json.getBoolean("DMA"))
options.disableDma()

if(!json.getBoolean("IpAddress"))
options.disableIpAddress()

if(!json.getBoolean("Language"))
options.disableLanguage()

if(!json.getBoolean("LatLng"))
options.disableLatLng()

if(!json.getBoolean("OsName"))
options.disableOsName()

if(!json.getBoolean("OsVersion"))
options.disableOsVersion()

if(!json.getBoolean("Platform"))
options.disablePlatform()

if(!json.getBoolean("Region"))
options.disableRegion()

if(!json.getBoolean("VersionName"))
options.disableVersionName()

client.setTrackingOptions(options)
result.success("setTrackingOptions called..")
}

else -> {
result.notImplemented()
Expand Down
72 changes: 72 additions & 0 deletions ios/Classes/SwiftAmplitudeFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,78 @@ import Amplitude
Amplitude.instance(withName: instanceName).setServerZone(ampServerZone, updateServerUrl: updateServerUrl)
result(true)


// TrackingOptions
case "setTrackingOptions":
let options = AMPTrackingOptions()

if !(args["Carrier"] as! Bool) {
options.disableCarrier()
}

if !(args["City"] as! Bool) {
options.disableCity()
}

if !(args["Country"] as! Bool) {
options.disableCountry()
}

if !(args["DeviceManufacturer"] as! Bool) {
options.disableDeviceManufacturer()
}

if !(args["DeviceModel"] as! Bool) {
options.disableDeviceModel()
}

if !(args["DMA"] as! Bool) {
options.disableDMA()
}

if !(args["IDFA"] as! Bool) {
options.disableIDFA()
}

if !(args["IDFV"] as! Bool) {
options.disableIDFV()
}

if !(args["IpAddress"] as! Bool) {
options.disableIPAddress()
}

if !(args["Language"] as! Bool) {
options.disableLanguage()
}

if !(args["LatLng"] as! Bool) {
options.disableLatLng()
}

if !(args["OsName"] as! Bool) {
options.disableOSName()
}

if !(args["OsVersion"] as! Bool) {
options.disableOSVersion()
}

if !(args["Platform"] as! Bool) {
options.disablePlatform()
}

if !(args["Region"] as! Bool) {
options.disableRegion()
}

if !(args["VersionName"] as! Bool) {
options.disableVersionName()
}

Amplitude.instance(withName: instanceName).setTrackingOptions(options)
result(true)

default:
result(FlutterMethodNotImplemented)
}
Expand Down
8 changes: 8 additions & 0 deletions lib/amplitude.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';

import 'package:amplitude_flutter/constants.dart';
import 'package:amplitude_flutter/identify.dart';
import 'package:amplitude_flutter/trackingOptions.dart';
import 'package:flutter/services.dart';

abstract class _Amplitude {
Expand Down Expand Up @@ -330,4 +331,11 @@ class Amplitude extends _Amplitude {

return await _channel.invokeMethod('setServerZone', jsonEncode(properties));
}

/// Set Amplitude Tracking Options, to enabla or disable some default properties.
Future<void> setTrackingOptions(TrackingOptions options) async {
Map<String, dynamic> properties = _baseProperties();
properties.addAll(options.getProperties());
return await _channel.invokeMethod('setTrackingOptions', jsonEncode(properties));
}
}
85 changes: 85 additions & 0 deletions lib/trackingOptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:flutter/foundation.dart';
import 'dart:convert';

enum TrackingOptionStatus { unknown, enabled, disabled }

// iOS only
// IDFA, IDFV
// Android only
// Adid, ApiLevel, AppSetId, DeviceBrand

class TrackingOptions {
final Map<String, dynamic> _options = {
'Adid': true,
'ApiLevel': true,
'AppSetId': true,
'Carrier': true,
'City': true,
'Country': true,
'DeviceBrand': true,
'DeviceManufacturer': true,
'DeviceModel': true,
'DMA': true,
'IDFA': true,
'IDFV': true,
'IpAddress': true,
'Language': true,
'LatLng': true,
'OsName': true,
'OsVersion': true,
'Platform': true,
'Region': true,
'VersionName': true
};

TrackingOptionStatus enableProperty(String key) {
if (_options.containsKey(key)) {
_options[key] = true;
return TrackingOptionStatus.enabled;
}
return TrackingOptionStatus.unknown;
}

TrackingOptionStatus disableProperty(String key) {
if (_options.containsKey(key)) {
_options[key] = false;
return TrackingOptionStatus.disabled;
}
return TrackingOptionStatus.unknown;
}

int enableProperties(List<String> optionKeys) {
int count = 0;
optionKeys.forEach((key) {
if (_options.containsKey(key)) {
_options[key] = true;
count++;
}
});
return count;
}

int disableProperties(List<String> optionKeys) {
int count = 0;
optionKeys.forEach((key) {
if (_options.containsKey(key)) {
_options[key] = false;
count++;
}
});
return count;
}

TrackingOptionStatus queryProperty(String key) {
if (_options.containsKey(key)) {
if(_options[key]!)
return TrackingOptionStatus.enabled;
return TrackingOptionStatus.disabled;
}
return TrackingOptionStatus.unknown;
}

Map<String, dynamic> getProperties(){
return new Map<String, dynamic>.from(_options);
}
}