Skip to content

Commit

Permalink
Merge pull request #38 from amplitude/regenerateDeviceID
Browse files Browse the repository at this point in the history
feat: Regenerate deviceId function added
  • Loading branch information
yuhao900914 authored Feb 10, 2021
2 parents ba5ef2e + 823f466 commit a15e67d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ class AmplitudeFlutterPlugin : FlutterPlugin, MethodCallHandler {
result.success("setServerUrl called..")
}

// Regenerate new deviceId
"regenerateDeviceId" -> {
val client = Amplitude.getInstance(instanceName)
client.regenerateDeviceId();
result.success("regenerateDeviceId called..")
}

// Event logging
"logEvent" -> {
val client = Amplitude.getInstance(instanceName)
Expand Down
10 changes: 6 additions & 4 deletions example/lib/my_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'group_identify_form.dart';
import 'identify_form.dart';
import 'revenue_form.dart';
import 'user_id_form.dart';
import 'regenerate_device.dart';

class MyApp extends StatefulWidget {
const MyApp(this.apiKey);
Expand All @@ -19,6 +20,7 @@ class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
String _message = '';
Amplitude analytics;
Expand All @@ -34,10 +36,8 @@ class _MyAppState extends State<MyApp> {
analytics.enableCoppaControl();
analytics.setUserId("test_user");
analytics.trackingSessionEvents(true);
analytics.logEvent('MyApp startup', eventProperties: {
'event_prop_1': 10,
'event_prop_2': true
});
analytics.logEvent('MyApp startup',
eventProperties: {'event_prop_1': 10, 'event_prop_2': true});
Map<String, dynamic> userProps = {
'date': '01.06.2020',
'name': 'Name',
Expand Down Expand Up @@ -80,6 +80,8 @@ class _MyAppState extends State<MyApp> {
children: <Widget>[
UserIdForm(),
divider,
RegenerateDeviceBtn(),
divider,
EventForm(),
divider,
IdentifyForm(),
Expand Down
22 changes: 22 additions & 0 deletions example/lib/regenerate_device.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:flutter/material.dart';

import 'app_state.dart';

class RegenerateDeviceBtn extends StatefulWidget {
@override
_DeviceState createState() => _DeviceState();
}

class _DeviceState extends State<RegenerateDeviceBtn> {
void onPress() {
AppState.of(context)
..analytics.regenerateDeviceId()
..setMessage('Regenerate DeviceId.');
}

@override
Widget build(BuildContext context) {
return RaisedButton(
child: const Text('Regenerate DeviceId'), onPressed: onPress);
}
}
5 changes: 5 additions & 0 deletions ios/Classes/SwiftAmplitudeFlutterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ import Amplitude

result(true)

// Regenerates a new random deviceId for current user
case "regenerateDeviceId":
Amplitude.instance(withName: instanceName).regenerateDeviceId()
result(true)

// Event logging
case "logEvent":
let eventType = args["eventType"] as! String
Expand Down
56 changes: 39 additions & 17 deletions lib/amplitude.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Amplitude extends _Amplitude {
_instances = <String, Amplitude>{};
}

return _instances.putIfAbsent(instanceName, () => new Amplitude(instanceName));
return _instances.putIfAbsent(
instanceName, () => new Amplitude(instanceName));
}

Amplitude(String instanceName) {
Expand All @@ -44,13 +45,15 @@ class Amplitude extends _Amplitude {
/// This can be used by any customer that does not want to collect IDFA, IDFV,
/// city, IP address and location tracking.
Future<void> enableCoppaControl() async {
return await _channel.invokeMethod('enableCoppaControl', jsonEncode(_baseProperties()));
return await _channel.invokeMethod(
'enableCoppaControl', jsonEncode(_baseProperties()));
}

/// Disable COPPA (Children's Online Privacy Protection Act) restrictions on
/// IDFA, IDFV, city, IP address and location tracking.
Future<void> disableCoppaControl() async {
return await _channel.invokeMethod('disableCoppaControl', jsonEncode(_baseProperties()));
return await _channel.invokeMethod(
'disableCoppaControl', jsonEncode(_baseProperties()));
}

/// Enables tracking opt out.
Expand All @@ -73,7 +76,8 @@ class Amplitude extends _Amplitude {
Map<String, dynamic> properties = _baseProperties();
properties['trackingSessionEvents'] = trackingSessionEvents;

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

/// If your app has its own login system that you want to track users with,
Expand All @@ -96,12 +100,22 @@ class Amplitude extends _Amplitude {
return await _channel.invokeMethod('setServerUrl', jsonEncode(properties));
}

/// Regenerates a new random deviceId for current user.
/// Note: this is not recommended unless you know what you are doing.
/// This can be used in conjunction with setUserId(null) to anonymize users after they log out.
/// With a null userId and a completely new deviceId, the current user would appear as a brand new user in dashboard.
Future<void> regenerateDeviceId() async {
return await _channel.invokeMethod(
'regenerateDeviceId', jsonEncode(_baseProperties()));
}

/// Dynamically adjust server URL
Future<void> setUseDynamicConfig(bool useDynamicConfig) async {
Map<String, dynamic> properties = _baseProperties();
properties['useDynamicConfig'] = useDynamicConfig;

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

/// Tracks an event. Events are saved locally.
Expand All @@ -112,7 +126,8 @@ class Amplitude extends _Amplitude {
/// [eventType] The name of the event you wish to track.
/// [eventProperties] You can attach additional data to any event by passing a
/// [Map] object with property: value pairs.
Future<void> logEvent(String eventType, {Map<String, dynamic> eventProperties, bool outOfSession}) async {
Future<void> logEvent(String eventType,
{Map<String, dynamic> eventProperties, bool outOfSession}) async {
Map<String, dynamic> properties = _baseProperties();
properties['eventType'] = eventType;
if (eventProperties != null) {
Expand All @@ -129,7 +144,8 @@ class Amplitude extends _Amplitude {
/// revenue on the Amplitude website, including average revenue per daily
/// active user (ARPDAU), 7, 30, and 90 day revenue, lifetime value (LTV)
/// estimates, and revenue by advertising campaign cohort and daily/weekly/monthly cohorts.
Future<void> logRevenue(String productIdentifier, int quantity, double price) async {
Future<void> logRevenue(
String productIdentifier, int quantity, double price) async {
Map<String, dynamic> properties = _baseProperties();
properties['productIdentifier'] = productIdentifier;
properties['quantity'] = quantity;
Expand All @@ -146,7 +162,8 @@ class Amplitude extends _Amplitude {
Map<String, dynamic> properties = _baseProperties();
properties['amount'] = amount;

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

/// Update user properties using operations provided via Identify API.
Expand Down Expand Up @@ -186,7 +203,9 @@ class Amplitude extends _Amplitude {

/// Use the Group Identify API to set or update properties of particular groups.
/// However, these updates will only affect events going forward.
Future<void> groupIdentify(String groupType, String groupName, Identify groupIdentify, {bool outOfSession = false}) async {
Future<void> groupIdentify(
String groupType, String groupName, Identify groupIdentify,
{bool outOfSession = false}) async {
Map<String, dynamic> properties = _baseProperties();
properties['groupType'] = groupType;
properties['groupName'] = groupName;
Expand All @@ -203,39 +222,42 @@ class Amplitude extends _Amplitude {
Map<String, dynamic> properties = _baseProperties();
properties['userProperties'] = userProperties;

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

/// Clears all properties that are tracked on the user level.
///
/// Note: This operation is irreversible!!
Future<void> clearUserProperties() async {
return await _channel.invokeMethod('clearUserProperties', jsonEncode(_baseProperties()));
return await _channel.invokeMethod(
'clearUserProperties', jsonEncode(_baseProperties()));
}

/// Upload all unsent events.
Future<void> uploadEvents() async {
return await _channel.invokeMethod('uploadEvents', jsonEncode(_baseProperties()));
return await _channel.invokeMethod(
'uploadEvents', jsonEncode(_baseProperties()));
}

Map<String, dynamic> _baseProperties() {
return {
'instanceName': _instanceName
};
return {'instanceName': _instanceName};
}

// Private bridging calls
Future<void> _setLibraryName(String libraryName) async {
Map<String, dynamic> properties = _baseProperties();
properties['libraryName'] = libraryName;

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

Future<void> _setLibraryVersion(String libraryVersion) async {
Map<String, dynamic> properties = _baseProperties();
properties['libraryVersion'] = libraryVersion;

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

0 comments on commit a15e67d

Please sign in to comment.