-
Notifications
You must be signed in to change notification settings - Fork 66
Refactor for buttons array with SurveyButton
class
#134
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 14 commits into
dart-lang:survey-handler-feature
from
eliasyishak:refactor-for-buttons-array
Aug 2, 2023
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
28404bf
Added newe `SurveyButton` class
eliasyishak 84863bb
Fix tests
eliasyishak 3298a66
Add documentation for enums
eliasyishak 814f673
Update sample_rate.dart
eliasyishak 0cd8348
Update tests to check for `SurveyButton` classes
eliasyishak 6d4b9a4
Remove enum for status of action
eliasyishak f68eb89
Use `snoozeForMinutes` instead of dismiss
eliasyishak 74b7791
Expose `SurveyButton`
eliasyishak e5b8a74
Fixing documentation for event class
eliasyishak 313226c
Order members in survey handler
eliasyishak 49b8014
Refactor to pass button to `surveyInteracted(..)`
eliasyishak 8475e53
`surveyButtonList` --> `buttonList` renaming
eliasyishak 2ac586f
Adding example file for how to use survey handler feature
eliasyishak fccdae1
Adding conditional check for url to display
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
import 'package:clock/clock.dart'; | ||
import 'package:file/file.dart'; | ||
import 'package:file/memory.dart'; | ||
|
||
import 'package:unified_analytics/src/constants.dart'; | ||
import 'package:unified_analytics/src/enums.dart'; | ||
import 'package:unified_analytics/src/survey_handler.dart'; | ||
import 'package:unified_analytics/unified_analytics.dart'; | ||
|
||
void main() async { | ||
late final MemoryFileSystem fs; | ||
late final Analytics analytics; | ||
late final Directory home; | ||
// We need to initialize with a fake clock since the surveys have | ||
// a period of time they are valid for | ||
await withClock(Clock.fixed(DateTime(2023, 3, 3, 12, 0)), () async { | ||
// Use a memory file system to repeatedly run this example | ||
// file with the test instance | ||
fs = MemoryFileSystem.test(style: FileSystemStyle.posix); | ||
home = fs.directory('home'); | ||
home.createSync(); | ||
|
||
// The purpose of `initialAnalytics` is so that the tool is able to | ||
// send events after its first run; this instance won't be used below | ||
// | ||
// ignore: invalid_use_of_visible_for_testing_member | ||
final initialAnalytics = Analytics.test( | ||
tool: DashTool.flutterTool, | ||
homeDirectory: home, | ||
measurementId: 'measurementId', | ||
apiSecret: 'apiSecret', | ||
dartVersion: 'dartVersion', | ||
fs: fs, | ||
platform: DevicePlatform.macos, | ||
); | ||
// The below command allows `DashTool.flutterTool` to send telemetry | ||
initialAnalytics.clientShowedMessage(); | ||
|
||
// ignore: invalid_use_of_visible_for_testing_member | ||
analytics = Analytics.test( | ||
tool: DashTool.flutterTool, | ||
homeDirectory: home, | ||
measurementId: 'measurementId', | ||
apiSecret: 'apiSecret', | ||
dartVersion: 'dartVersion', | ||
fs: fs, | ||
platform: DevicePlatform.macos, | ||
surveyHandler: FakeSurveyHandler.fromList( | ||
homeDirectory: home, | ||
fs: fs, | ||
initializedSurveys: [ | ||
Survey( | ||
uniqueId: 'uniqueId', | ||
startDate: DateTime(2023, 1, 1), | ||
endDate: DateTime(2023, 5, 31), | ||
description: 'description', | ||
snoozeForMinutes: 10, | ||
samplingRate: 1.0, | ||
conditionList: [], | ||
buttonList: [ | ||
SurveyButton( | ||
buttonText: 'View Survey', | ||
action: 'accept', | ||
promptRemainsVisible: false, | ||
url: 'http://example.com', | ||
), | ||
SurveyButton( | ||
buttonText: 'More Info', | ||
action: 'snooze', | ||
promptRemainsVisible: true, | ||
url: 'http://example2.com', | ||
), | ||
SurveyButton( | ||
buttonText: 'Dismiss Survey', | ||
action: 'dismiss', | ||
promptRemainsVisible: false, | ||
) | ||
], | ||
), | ||
], | ||
)); | ||
|
||
// Send one event to allow `LogFileStats` to not be null | ||
await analytics.send(Event.hotReloadTime(timeMs: 50)); | ||
}); | ||
|
||
// Each client of this package will be able to fetch all of | ||
// the available surveys with the below method | ||
// | ||
// Sample rate will be applied automatically; it also won't | ||
// fetch any surveys in the snooze period or if they have | ||
// been dismissed | ||
final surveyList = await analytics.fetchAvailableSurveys(); | ||
assert(surveyList.length == 1); | ||
|
||
// Grab the first and only survey to simulate displaying it to a user | ||
final survey = surveyList.first; | ||
print('Simulating displaying the survey with a print below:'); | ||
print('Survey id: ${survey.uniqueId}\n'); | ||
|
||
// Immediately after displaying the survey, the method below | ||
// should be run so that no other clients using this tool will show | ||
// it at the same time | ||
// | ||
// It will "snoozed" when the below is run as well as reported to | ||
// Google Analytics 4 that this survey was shown | ||
analytics.surveyShown(survey); | ||
|
||
// Get the file where this is persisted to show it getting updated | ||
final persistedSurveyFile = home | ||
.childDirectory(kDartToolDirectoryName) | ||
.childFile(kDismissedSurveyFileName); | ||
print('The contents of the json file ' | ||
'after invoking `analytics.surveyShown(survey);`'); | ||
print('${persistedSurveyFile.readAsStringSync()}\n'); | ||
|
||
// Change the index below to decide which button to simulate pressing | ||
// | ||
// 0 - accept | ||
// 1 - snooze | ||
// 2 - dismiss | ||
final selectedButtonIndex = 1; | ||
assert([0, 1, 2].contains(selectedButtonIndex)); | ||
|
||
// Get the survey button by index that will need to be passed along with | ||
// the survey to simulate an interaction with the survey | ||
final selectedSurveyButton = survey.buttonList[selectedButtonIndex]; | ||
print('The simulated button pressed was: ' | ||
'"${selectedSurveyButton.buttonText}" ' | ||
'(action = ${selectedSurveyButton.action})\n'); | ||
|
||
// The below method will handle whatever action the button | ||
analytics.surveyInteracted( | ||
survey: survey, | ||
surveyButton: selectedSurveyButton, | ||
); | ||
|
||
// Conditional to check if there is a URl to route to | ||
if (selectedSurveyButton.url != null) { | ||
print('***This button also has a survey URL link ' | ||
'to route to at "${selectedSurveyButton.url}"***\n'); | ||
} | ||
|
||
// Conditional to check what simulating a popup to stay up | ||
if (selectedSurveyButton.promptRemainsVisible) { | ||
print('***This button has its promptRemainsVisible field set to `true` ' | ||
'so this simulates what seeing a pop up again would look like***\n'); | ||
} | ||
Comment on lines
+144
to
+148
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is useful info for debugging, printing when there's a URL that would be opened might be useful too. |
||
|
||
print('The contents of the json file ' | ||
'after invoking ' | ||
'`analytics.surveyInteracted(survey: survey, ' | ||
'surveyButton: selectedSurveyButton);`'); | ||
print('${persistedSurveyFile.readAsStringSync()}\n'); | ||
|
||
// Demonstrating that the survey doesn't get returned again | ||
print('Attempting to fetch surveys again will result in an empty list'); | ||
print(await analytics.fetchAvailableSurveys()); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DanTup this example file should make it easier for us to discuss any changes to the workflow (if any are needed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bwilkerson also an fyi if this example file workflow... from the analysis server side, does this workflow make sense to you?