-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathanalytics_client.dart
More file actions
133 lines (123 loc) · 4.17 KB
/
analytics_client.dart
File metadata and controls
133 lines (123 loc) · 4.17 KB
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
import 'package:equatable/equatable.dart';
import 'package:stream_feed/src/core/api/analytics_api.dart';
import 'package:stream_feed/src/core/http/stream_http_client.dart';
import 'package:stream_feed/src/core/http/token.dart';
import 'package:stream_feed/src/core/models/event.dart';
import 'package:stream_feed/src/core/util/token_helper.dart';
/// {@template analytics}
/// Sends out analytic events to the Stream service.
/// We recommend tracking every event for each user. This allows you to
/// gain a better understanding of that user's interests.
/// Common examples include:
/// - Clicking on a link
/// - Liking or commenting
/// - Sharing an activity
/// - Viewing another user's profile page
/// - Searching for a certain user/content/topic/etc.
/// {@endtemplate}
// ignore: must_be_immutable
class StreamAnalytics extends Equatable {
/// [StreamAnalytics] constructor:
///
/// {@macro analytics}
StreamAnalytics(
String apiKey, {
this.secret,
this.userToken,
AnalyticsAPI? analytics,
StreamHttpClientOptions? options,
}) : assert(
userToken != null || secret != null,
'At least a secret or userToken must be provided',
),
_analytics = analytics ?? AnalyticsAPI(apiKey, options: options);
final AnalyticsAPI _analytics;
final String? secret;
final Token? userToken;
/// Data related to the user
UserData? userData;
//TODO: get user?
/// Set user id and alias
void setUser({required String id, required String alias}) =>
userData = UserData(id, alias);
List<T> _validateAndNormalizeUserData<T extends Event>(List<T> events) =>
events.map((e) {
if (e.userData != null || userData != null) {
return e.copyWith(userData: e.userData ?? userData) as T;
}
throw Exception(
'UserData should be in each event or '
'set the default with AnalyticsClient.setUser()',
);
}).toList(growable: false);
/// {@template trackImpression}
/// Tracks the amount of times an Activity has been viewed, by which
/// users, and more.
///
/// Check out the [Impression] object to view the full details of what
/// gets tracked.
///
/// {@endtemplate}
///
/// # Example
///
/// A user is viewing a page:
/// ```dart
/// final analytics = AnalyticsClient(apiKey, secret: secret);
/// await analytics.trackImpression(
/// Impression(
/// contentList: [
/// {"foreign_id": "tweet:34349698"}
/// ],
/// userData: UserData("test", "test"),
/// feedId: FeedId("flat", "tommaso"),
/// location: "profile_page",
/// ),
/// );
/// ```
Future<void> trackImpression(Impression impression) =>
trackImpressions([impression]);
/// Send [Impression] events
Future<void> trackImpressions(List<Impression> impressions) async {
final impressionDataList = _validateAndNormalizeUserData(impressions);
final token =
userToken ?? TokenHelper.buildAnalytics(secret!, TokenAction.write);
await _analytics.trackImpressions(token, impressionDataList);
}
/// {@template trackEngagement}
/// Tracks the interactions users have had with an Activity.
///
/// Check out the [Engagement] class for the full details of what gets
/// tracked.
/// {@endtemplate}
///
/// # Example
///
/// A user is interacting with a content item:
/// ```dart
/// final analytics = AnalyticsClient(apiKey, userToken: token);
/// await analytics.trackEngagement(
/// Engagement(
/// content: {"foreign_id": "tweet:34349698"},
/// label: "click",
/// userData: UserData("test", "test"),
/// feedId: FeedId("user", "thierry"),
/// ),
/// );
/// ```
Future<void> trackEngagement(Engagement engagement) =>
trackEngagements([engagement]);
/// Send [Engagement] event
Future<void> trackEngagements(List<Engagement> engagements) {
final engagementDataList = _validateAndNormalizeUserData(engagements);
final token =
userToken ?? TokenHelper.buildAnalytics(secret!, TokenAction.write);
return _analytics.trackEngagements(token, engagementDataList);
}
@override
List<Object?> get props => [
secret,
userToken,
userData,
];
}