Skip to content

Commit ecd5bdd

Browse files
authored
1 adding dash analytics (#2)
Includes the initial commit for the dash_analytics package – additional work includes adding more `DashTool` enum values as deciding if we want to standardize the `eventData` within the `Analytics.sendEvent(...)` method
1 parent 73f15e4 commit ecd5bdd

24 files changed

+2586
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.DS_Store

CODEOWNERS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The CODEOWNERS file helps to define individuals or teams that responsible
2+
# for code within the repository
3+
# https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners
4+
5+
/pkgs/dash_analytics/ @eliasyishak

pkgs/dash_analytics/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build outputs.
6+
build/
7+
8+
# Omit committing pubspec.lock for library packages; see
9+
# https://dart.dev/guides/libraries/private-files#pubspeclock.
10+
pubspec.lock
11+
12+
.vscode/
13+
coverage/
14+
*.DS_Store

pkgs/dash_analytics/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial version.

pkgs/dash_analytics/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2023, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/dash_analytics/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## What's This?
2+
3+
This package is intended to be used on Dash (Flutter, Dart, etc.) related tooling only.
4+
It provides APIs to send events to Google Analytics using the Measurement Protocol.
5+
6+
This is not intended to be general purpose or consumed by the community. It is responsible for toggling analytics collection for Dash related tooling on each developer's machine.
7+
8+
## Using This Package As A Dash Tool
9+
10+
Refer to the [guide](USAGE_GUIDE.md)

pkgs/dash_analytics/USAGE_GUIDE.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
This package is intended to be used on Dash (Flutter, Dart, etc.) related tooling only.
2+
It provides APIs to send events to Google Analytics using the Measurement Protocol.
3+
4+
## Usage
5+
6+
To get started using this package, import at the entrypoint dart file and
7+
initialize with the required parameters
8+
9+
```dart
10+
import 'dash_analytics';
11+
12+
// Constants that should be resolved by the client using package
13+
final DashTool tool = DashTool.flutterTools; // Restricted to enum provided by package
14+
final String measurementId = 'xxxxxxxxxxxx'; // To be provided to client
15+
final String apiSecret = 'xxxxxxxxxxxx'; // To be provided to client
16+
17+
// Values that need to be provided by the client that may
18+
// need to be calculated
19+
final String branch = ...;
20+
final String flutterVersion = ...;
21+
final String dartVersion = ...;
22+
23+
// Initialize the [Analytics] class with the required parameters;
24+
// preferably outside of the [main] method
25+
final Analytics analytics = Analytics(
26+
tool: tool,
27+
measurementId: measurementId,
28+
apiSecret: apiSecret,
29+
branch: branch,
30+
flutterVersion: flutterVersion,
31+
dartVersion: dartVersion,
32+
);
33+
34+
// Timing a process and sending the event
35+
void main() {
36+
DateTime start = DateTime.now();
37+
int count = 0;
38+
39+
// Example of long running process
40+
for (int i = 0; i < 2000; i++) {
41+
count += i;
42+
}
43+
44+
// Calculate the metric to send
45+
final int runTime = DateTime.now().difference(start).inMilliseconds;
46+
47+
// Generate the body for the event data
48+
final Map<String, int> eventData = {
49+
'time_ns': runTime,
50+
};
51+
52+
// Choose one of the enum values for [DashEvent] which should
53+
// have all possible events; if not there, open an issue for the
54+
// team to add
55+
final DashEvent eventName = ...; // Select appropriate DashEvent enum value
56+
57+
// Make a call to the [Analytics] api to send the data
58+
analytics.sendEvent(
59+
eventName: eventName,
60+
eventData: eventData,
61+
);
62+
63+
// Close the client connection on exit
64+
analytics.close();
65+
}
66+
```
67+
68+
## Opting In and Out of Analytics Collection
69+
70+
It will be important for each Dash tool to expose a trivial method to
71+
disabling or enabling analytics collection. Based on how the user interacts
72+
with the tool, this can be done through the CLI, IDE, etc. The Dash tool will
73+
then pass a boolean to an API exposed by the package as shown below
74+
75+
```dart
76+
// Begin by initializing the class
77+
final Analytics analytics = Analytics(...);
78+
79+
// The boolean below simulates the user deciding to opt-out
80+
// of Analytics collection
81+
final bool status = false;
82+
83+
// Call the method to pass the boolean
84+
analytics.setTelemetry(status);
85+
```
86+
87+
## Informing Users About Analytics Opt-In Status
88+
89+
When a user first uses any Dash tool with this package enabled, they
90+
will be enrolled into Analytics collection. It will be the responsiblity
91+
of the Dash tool using this package to display the proper Analytics messaging
92+
and inform them on how to Opt-Out of Analytics collection if they wish. The
93+
package will expose APIs that will make it easy to configure Opt-In status.
94+
95+
```dart
96+
// Begin by initializing the class
97+
final Analytics analytics = Analytics(...);
98+
99+
// This should be performed every time the Dash tool starts up
100+
if (analytics.shouldShowMessage) {
101+
102+
// How each Dash tool displays the message will be unique,
103+
// print statement used for trivial usage example
104+
print(analytics.toolsMessage);
105+
}
106+
```
107+
108+
## Checking User Opt-In Status
109+
110+
Some Dash tools may need to know if the user has opted in for Analytics
111+
collection in order to enable additional functionality. The example below
112+
shows how to check the status
113+
114+
```dart
115+
// Begin by initializing the class
116+
final Analytics analytics = Analytics(...);
117+
118+
// This getter will return a boolean showing the status;
119+
// print statement used for trivial usage example
120+
print('This user's status: ${analytics.telemetryEnabled}'); // true if opted-in
121+
```
122+
123+
## Advanced Usage: Querying Locally Persisted Logs
124+
125+
This package enables dash tools to persist the events that have been
126+
sent to Google Analytics for logging by default. This can be very helpful if
127+
dash tools would like to understand the user's activity level across all
128+
dash related tooling. For example, if querying the locally persisted logs
129+
shows that the user has not been active for N number of days, a dash tool that
130+
works within an IDE can prompt the user with a survey to understand why their
131+
level of activity has dropped.
132+
133+
The snippet below shows how to invoke the query and a sample response
134+
135+
```dart
136+
// Begin by initializing the class
137+
final Analytics analytics = Analytics(...);
138+
139+
// Printing the query results returns json formatted
140+
// string to view; data can also be accessed through
141+
// [LogFileStats] getters
142+
print(analytics.logFileStats());
143+
144+
// Prints out the below
145+
// {
146+
// "startDateTime": "2023-02-08 15:07:10.293728",
147+
// "endDateTime": "2023-02-08 15:07:10.299678",
148+
// "sessionCount": 1,
149+
// "flutterChannelCount": 1,
150+
// "toolCount": 1
151+
// }
152+
```
153+
154+
Explanation of the each key above
155+
156+
- startDateTime: the earliest event that was sent
157+
- endDateTime: the latest, most recent event that was sent
158+
- sessionCount: count of sessions; sessions have a minimum time of 30 minutes
159+
- flutterChannelCount: count of flutter channels (can be 0 if developer is a Dart dev only)
160+
- toolCount: count of the dash tools sending analytics
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
linter:
19+
rules:
20+
- always_declare_return_types
21+
- always_specify_types
22+
- camel_case_types
23+
- prefer_single_quotes
24+
- unawaited_futures
25+
26+
# analyzer:
27+
# exclude:
28+
# - path/to/excluded/files/**
29+
30+
# For more information about the core and recommended set of lints, see
31+
# https://dart.dev/go/core-lints
32+
33+
# For additional information about configuring this file, see
34+
# https://dart.dev/guides/language/analysis-options
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
# Generate `coverage/lcov.info` file
4+
flutter test --coverage
5+
6+
# Generate HTML report
7+
# Note: on macOS you need to have lcov installed on your system (`brew install lcov`) to use this:
8+
genhtml coverage/lcov.info -o coverage/html
9+
10+
# Open the report
11+
open coverage/html/index.html
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:dash_analytics/dash_analytics.dart';
6+
7+
final String measurementId = 'G-N1NXG28J5B';
8+
final String apiSecret = '4yT8__oER3Cd84dtx6r-_A';
9+
10+
// Globally instantiate the analytics class at the entry
11+
// point of the tool
12+
final Analytics analytics = Analytics(
13+
tool: DashTool.flutterTools,
14+
measurementId: measurementId,
15+
apiSecret: apiSecret,
16+
flutterChannel: 'ey-test-channel',
17+
flutterVersion: 'Flutter 3.6.0-7.0.pre.47',
18+
dartVersion: 'Dart 2.19.0',
19+
);
20+
21+
void main() {
22+
DateTime start = DateTime.now();
23+
print('###### START ###### $start');
24+
25+
print(analytics.telemetryEnabled);
26+
analytics.sendEvent(
27+
eventName: DashEvent.hotReloadTime,
28+
eventData: <String, int>{'time_ns': 345},
29+
);
30+
print(analytics.logFileStats());
31+
analytics.close();
32+
33+
DateTime end = DateTime.now();
34+
print(
35+
'###### DONE ###### ${DateTime.now()} ${end.difference(start).inMilliseconds}ms');
36+
}

0 commit comments

Comments
 (0)