-
Notifications
You must be signed in to change notification settings - Fork 67
Send event for surveys shown and surveys dismissed #133
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
Merged
eliasyishak
merged 6 commits into
dart-lang:survey-handler-feature
from
eliasyishak:send-event-for-dismissed-surveys
Jul 28, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2208453
Added enum and event constructor survey actions
eliasyishak 45f3ea6
Fix format errors
eliasyishak 1716ed0
Using two events for survey shown and survey action
eliasyishak b6c3c3f
Created mock class to confirm events are sent
eliasyishak a2d1da5
Clean up constructors
eliasyishak 5302d53
Fix nits
eliasyishak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:clock/clock.dart'; | ||
import 'package:file/file.dart'; | ||
import 'package:file/memory.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'package:unified_analytics/src/enums.dart'; | ||
import 'package:unified_analytics/src/survey_handler.dart'; | ||
import 'package:unified_analytics/unified_analytics.dart'; | ||
|
||
import 'src/fake_analytics.dart'; | ||
|
||
void main() { | ||
// The fake analytics instance can be used to ensure events | ||
// are being sent when invoking methods on the `Analytics` instance | ||
|
||
late FakeAnalytics fakeAnalytics; | ||
late FileSystem fs; | ||
late Directory homeDirectory; | ||
|
||
/// Survey to load into the fake instance to fetch | ||
/// | ||
/// The 1.0 sample rate means that this will always show | ||
/// up from the method to fetch available surveys | ||
final testSurvey = Survey( | ||
uniqueId: 'uniqueId', | ||
url: 'url', | ||
startDate: DateTime(2022, 1, 1), | ||
endDate: DateTime(2022, 12, 31), | ||
description: 'description', | ||
dismissForMinutes: 10, | ||
moreInfoUrl: 'moreInfoUrl', | ||
samplingRate: 1.0, // 100% sample rate | ||
conditionList: <Condition>[], | ||
); | ||
|
||
/// Test event that will need to be sent since surveys won't | ||
/// be fetched until at least one event is logged in the persisted | ||
/// log file on disk | ||
final testEvent = Event.hotReloadTime(timeMs: 10); | ||
|
||
setUp(() async { | ||
fs = MemoryFileSystem.test(style: FileSystemStyle.posix); | ||
homeDirectory = fs.directory('home'); | ||
|
||
final initialAnalytics = Analytics.test( | ||
tool: DashTool.flutterTool, | ||
homeDirectory: homeDirectory, | ||
measurementId: 'measurementId', | ||
apiSecret: 'apiSecret', | ||
dartVersion: 'dartVersion', | ||
toolsMessageVersion: 1, | ||
fs: fs, | ||
platform: DevicePlatform.macos, | ||
); | ||
initialAnalytics.clientShowedMessage(); | ||
|
||
// Recreate a second instance since events cannot be sent on | ||
// the first run | ||
withClock(Clock.fixed(DateTime(2022, 3, 3)), () { | ||
fakeAnalytics = FakeAnalytics( | ||
tool: DashTool.flutterTool, | ||
homeDirectory: homeDirectory, | ||
dartVersion: 'dartVersion', | ||
platform: DevicePlatform.macos, | ||
toolsMessageVersion: 1, | ||
fs: fs, | ||
surveyHandler: FakeSurveyHandler.fromList( | ||
homeDirectory: homeDirectory, | ||
fs: fs, | ||
initializedSurveys: [testSurvey], | ||
), | ||
enableAsserts: true, | ||
); | ||
}); | ||
}); | ||
|
||
test('event sent when survey shown', () async { | ||
// Fire off the test event to allow surveys to be fetched | ||
await fakeAnalytics.send(testEvent); | ||
|
||
final surveyList = await fakeAnalytics.fetchAvailableSurveys(); | ||
expect(surveyList.length, 1); | ||
expect(fakeAnalytics.sentEvents.length, 1, | ||
reason: 'Only one event sent from the test event above'); | ||
|
||
final survey = surveyList.first; | ||
expect(survey.uniqueId, 'uniqueId'); | ||
|
||
// Simulate the survey being shown | ||
fakeAnalytics.surveyShown(survey); | ||
|
||
expect(fakeAnalytics.sentEvents.length, 2); | ||
expect(fakeAnalytics.sentEvents.last.eventName, DashEvent.surveyShown); | ||
expect(fakeAnalytics.sentEvents.last.eventData, {'surveyId': 'uniqueId'}); | ||
}); | ||
|
||
test('event sent when survey accepted', () async { | ||
// Fire off the test event to allow surveys to be fetched | ||
await fakeAnalytics.send(testEvent); | ||
|
||
final surveyList = await fakeAnalytics.fetchAvailableSurveys(); | ||
expect(surveyList.length, 1); | ||
expect(fakeAnalytics.sentEvents.length, 1, | ||
reason: 'Only one event sent from the test event above'); | ||
|
||
final survey = surveyList.first; | ||
expect(survey.uniqueId, 'uniqueId'); | ||
|
||
// Simulate the survey being shown | ||
fakeAnalytics.dismissSurvey(survey: survey, surveyAccepted: true); | ||
|
||
expect(fakeAnalytics.sentEvents.length, 2); | ||
expect(fakeAnalytics.sentEvents.last.eventName, DashEvent.surveyAction); | ||
expect(fakeAnalytics.sentEvents.last.eventData, | ||
{'surveyId': 'uniqueId', 'status': 'accepted'}); | ||
}); | ||
|
||
test('event sent when survey rejected', () async { | ||
// Fire off the test event to allow surveys to be fetched | ||
await fakeAnalytics.send(testEvent); | ||
|
||
final surveyList = await fakeAnalytics.fetchAvailableSurveys(); | ||
expect(surveyList.length, 1); | ||
expect(fakeAnalytics.sentEvents.length, 1, | ||
reason: 'Only one event sent from the test event above'); | ||
|
||
final survey = surveyList.first; | ||
expect(survey.uniqueId, 'uniqueId'); | ||
|
||
// Simulate the survey being shown | ||
fakeAnalytics.dismissSurvey(survey: survey, surveyAccepted: false); | ||
|
||
expect(fakeAnalytics.sentEvents.length, 2); | ||
expect(fakeAnalytics.sentEvents.last.eventName, DashEvent.surveyAction); | ||
expect(fakeAnalytics.sentEvents.last.eventData, | ||
{'surveyId': 'uniqueId', 'status': 'dismissed'}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:http/http.dart'; | ||
|
||
import 'package:unified_analytics/src/analytics.dart'; | ||
import 'package:unified_analytics/src/asserts.dart'; | ||
import 'package:unified_analytics/src/event.dart'; | ||
import 'package:unified_analytics/src/ga_client.dart'; | ||
import 'package:unified_analytics/src/log_handler.dart'; | ||
import 'package:unified_analytics/src/utils.dart'; | ||
|
||
class FakeAnalytics extends AnalyticsImpl { | ||
final List<Event> sentEvents = []; | ||
final LogHandler _logHandler; | ||
final FakeGAClient _gaClient; | ||
final String _clientId = 'hard-coded-client-id'; | ||
|
||
/// Class to use when you want to see which events were sent | ||
FakeAnalytics({ | ||
required super.tool, | ||
required super.homeDirectory, | ||
required super.dartVersion, | ||
required super.platform, | ||
required super.toolsMessageVersion, | ||
required super.fs, | ||
required super.surveyHandler, | ||
required super.enableAsserts, | ||
super.flutterChannel, | ||
super.flutterVersion, | ||
FakeGAClient super.gaClient = const FakeGAClient(), | ||
}) : _logHandler = LogHandler(fs: fs, homeDirectory: homeDirectory), | ||
_gaClient = gaClient; | ||
|
||
@override | ||
Future<Response>? send(Event event) { | ||
if (!okToSend) return null; | ||
|
||
// Construct the body of the request | ||
final body = generateRequestBody( | ||
clientId: _clientId, | ||
eventName: event.eventName, | ||
eventData: event.eventData, | ||
userProperty: userProperty, | ||
); | ||
|
||
checkBody(body); | ||
|
||
_logHandler.save(data: body); | ||
|
||
// Using this list to validate that events are being sent | ||
// for internal methods in the `Analytics` instance | ||
sentEvents.add(event); | ||
return _gaClient.sendData(body); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.