-
Notifications
You must be signed in to change notification settings - Fork 298
/
facebook_app_events.dart
213 lines (184 loc) · 7.36 KB
/
facebook_app_events.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
const channelName = 'flutter.oddbit.id/facebook_app_events';
class FacebookAppEvents {
static const _channel = MethodChannel(channelName);
// See: https://github.com/facebook/facebook-android-sdk/blob/master/facebook-core/src/main/java/com/facebook/appevents/AppEventsConstants.java
static const eventNameActivatedApp = 'fb_mobile_activate_app';
static const eventNameDeactivatedApp = 'fb_mobile_deactivate_app';
static const eventNameCompletedRegistration =
'fb_mobile_complete_registration';
static const eventNameViewedContent = 'fb_mobile_content_view';
static const eventNameRated = 'fb_mobile_rate';
static const _paramNameValueToSum = "_valueToSum";
static const paramNameRegistrationMethod = "fb_registration_method";
/// Parameter key used to specify a generic content type/family for the logged event, e.g.
/// "music", "photo", "video". Options to use will vary depending on the nature of the app.
static const paramNameContentType = "fb_content_type";
/// Parameter key used to specify data for the one or more pieces of content being logged about.
/// Data should be a JSON encoded string.
/// Example:
/// "[{\"id\": \"1234\", \"quantity\": 2, \"item_price\": 5.99}, {\"id\": \"5678\", \"quantity\": 1, \"item_price\": 9.99}]"
static const paramNameContent = "fb_content";
/// Parameter key used to specify an ID for the specific piece of content being logged about.
/// This could be an EAN, article identifier, etc., depending on the nature of the app.
static const paramNameContentId = "fb_content_id";
/// Clears the current user data
Future<void> clearUserData() {
return _channel.invokeMethod<void>('clearUserData');
}
/// Clears the currently set user id.
Future<void> clearUserID() {
return _channel.invokeMethod<void>('clearUserID');
}
/// Explicitly flush any stored events to the server.
Future<void> flush() {
return _channel.invokeMethod<void>('flush');
}
/// Explicitly flush any stored events to the server.
Future<void> getApplicationId() {
return _channel.invokeMethod<void>('getApplicationId');
}
/// Log an app event with the specified [name] and the supplied [parameters] value.
Future<void> logEvent({
@required String name,
Map<String, dynamic> parameters,
double valueToSum,
}) {
final args = <String, dynamic>{
'name': name,
'parameters': parameters,
_paramNameValueToSum: valueToSum,
};
return _channel.invokeMethod<void>('logEvent', _filterOutNulls(args));
}
/// Sets user data to associate with all app events.
/// All user data are hashed and used to match Facebook user from this
/// instance of an application. The user data will be persisted between
/// application instances.
Future<void> setUserData({
String email,
String firstName,
String lastName,
String phone,
String dateOfBirth,
String gender,
String city,
String state,
String zip,
String country,
}) {
final args = <String, dynamic>{
'email': email,
'firstName': firstName,
'lastName': lastName,
'phone': phone,
'dateOfBirth': dateOfBirth,
'gender': gender,
'city': city,
'state': state,
'zip': zip,
'country': country,
};
return _channel.invokeMethod<void>('setUserData', args);
}
/// Logs an app event that tracks that the application was open via Push Notification.
Future<void> logPushNotificationOpen({
@required Map<String, dynamic> payload,
String action,
}) {
final args = <String, dynamic>{
'payload': payload,
'action': action,
};
return _channel.invokeMethod<void>('logPushNotificationOpen', args);
}
/// Sets a user [id] to associate with all app events.
/// This can be used to associate your own user id with the
/// app events logged from this instance of an application.
/// The user ID will be persisted between application instances.
Future<void> setUserID(String id) {
return _channel.invokeMethod<void>('setUserID', id);
}
/// Update user properties as provided by a map of [parameters]
Future<void> updateUserProperties({
@required Map<String, dynamic> parameters,
String applicationId,
}) {
final args = <String, dynamic>{
'parameters': parameters,
'applicationId': applicationId,
};
return _channel.invokeMethod<void>('updateUserProperties', args);
}
// Below are shorthand implementations of the predefined app event constants
/// Log this event when an app is being activated.
///
/// See: https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventsconstants.html/#eventnameactivatedapp
Future<void> logActivatedApp() {
return logEvent(name: eventNameActivatedApp);
}
/// Log this event when an app is being deactivated.
///
/// See: https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventsconstants.html/#eventnamedeactivatedapp
Future<void> logDeactivatedApp() {
return logEvent(name: eventNameDeactivatedApp);
}
/// Log this event when the user has completed registration with the app.
/// Parameter [registrationMethod] is used to specify the method the user has
/// used to register for the app, e.g. "Facebook", "email", "Google", etc.
/// See: https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventsconstants.html/#eventnamecompletedregistration
Future<void> logCompletedRegistration({String registrationMethod}) {
return logEvent(
name: eventNameCompletedRegistration,
parameters: {
paramNameRegistrationMethod: registrationMethod,
},
);
}
/// Log this event when the user has rated an item in the app.
///
/// See: https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventsconstants.html/#eventnamerated
Future<void> logRated({double valueToSum}) {
return logEvent(
name: eventNameRated,
valueToSum: valueToSum,
);
}
/// Log this event when the user has viewed a form of content in the app.
///
/// See: https://developers.facebook.com/docs/reference/androidsdk/current/facebook/com/facebook/appevents/appeventsconstants.html/#eventnameviewedcontent
Future<void> logViewContent({
Map<String, dynamic> content,
String id,
String type,
}) {
return logEvent(
name: eventNameViewedContent,
parameters: {
paramNameContent: content,
paramNameContentId: id,
paramNameContentType: type,
},
);
}
/// Creates a new map containing all of the key/value pairs from [parameters]
/// except those whose value is `null`.
Map<String, dynamic> _filterOutNulls(Map<String, dynamic> parameters) {
final Map<String, dynamic> filtered = <String, dynamic>{};
parameters.forEach((String key, dynamic value) {
if (value != null) {
filtered[key] = value;
}
});
return filtered;
}
/// Re-enables auto logging of app events after user consent
/// if disabled for GDPR-compliance.
///
/// See: https://developers.facebook.com/docs/app-events/gdpr-compliance
Future<void> setAutoLogAppEventsEnabled(bool enabled) {
return _channel.invokeMethod<void>('setAutoLogAppEventsEnabled', enabled);
}
}