Created
September 11, 2013 02:52
-
-
Save m-gagne/6518787 to your computer and use it in GitHub Desktop.
Windows 8 HTML5/JavaScript code sample that asks for a review of the app after 5 launches per app version
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
// ------------------------------------------------------------------- | |
// Ask for a review after 5 starts of a new version of the Application | |
// ------------------------------------------------------------------- | |
// Variable to hold the number of time the app started | |
var startedCount = 0; | |
// Variable to hold if the user actually accepted to rate the application | |
var reviewedBefore = false; | |
// Variables to hold the current and the last version number that was started in a string | |
var currentPackage = Windows.ApplicationModel.Package; | |
var currentVersion = currentPackage.current.id.version.major + " - " + | |
currentPackage.current.id.version.minor + " - " + | |
currentPackage.current.id.version.build + " - " + | |
currentPackage.current.id.version.revision; | |
var lastVersionStarted = currentVersion; | |
// Check if the variables were stored in the Roaming Settings before loading them | |
var appData = Windows.Storage.ApplicationData.current; | |
var roamingSettings = appData.roamingSettings; | |
if (roamingSettings.values.hasKey("startedCount")) | |
{ | |
// If so, read it in the variable | |
startedCount = roamingSettings.values["startedCount"]; | |
} | |
if (roamingSettings.values.hasKey("reviewedBefore")) { | |
// If so, read it in the variable | |
reviewedBefore = roamingSettings.values["reviewedBefore"]; | |
} | |
if (roamingSettings.values.hasKey("lastVersionStarted")) { | |
// If so, read it in the variable | |
lastVersionStarted = roamingSettings.values["lastVersionStarted"]; | |
} | |
// If the current version is equal to the last version started | |
if (currentVersion.localeCompare(lastVersionStarted) == 0) | |
// Increment the started count by 1 | |
startedCount++; | |
else | |
// Reset the count because the new version might have fixed some problems or be better | |
startedCount = 0; | |
// And then save it in the Roaming Settings. Note that if the value pair was not | |
// created before, the method will create it automatically | |
roamingSettings.values["startedCount"] = startedCount; | |
roamingSettings.values["lastVersionStarted"] = currentVersion; | |
// If the app was started 5 times | |
if (startedCount == 5) | |
{ | |
// Change the msg based on the fact that the application was reviewed before or not. | |
var msg; | |
if (!reviewedBefore) | |
msg = "Thank you for using [App Name] for a while now, would you like to review this app?"; | |
else | |
msg = "Thank you for your ongoing use of [App Name]. We made changes since the last time you were asked to review it. Would you mind telling us what you think again?"; | |
// Create a MessageDialog requesting a review | |
var md = new Windows.UI.Popups.MessageDialog(msg, "Please review my app"); | |
// Build the 3 potential responses using the Commands collection | |
// If they say yes, send them to the store | |
md.commands.append(new Windows.UI.Popups.UICommand( | |
"Yes", | |
function (command) { | |
// Change the status to track the fact that they agreed to a review | |
reviewedBefore = true; | |
// and store it | |
roamingSettings.values["reviewedBefore"] = reviewedBefore; | |
// Find the FamilyName of the app package | |
var familyName = Windows.ApplicationModel.Package.current.id.familyName; | |
// Navigate to the store Review Page for the Application | |
// *** Note that the next line will launch the store app but will not find your app until | |
// *** it is published. | |
var uri = new Windows.Foundation.Uri("ms-windows-store:REVIEW?PFN={0}", familyName); | |
Windows.System.Launcher.launchUriAsync(uri); | |
})); | |
// If they say no, just close the dialog box | |
md.commands.append(new Windows.UI.Popups.UICommand( | |
"No")); | |
// If they say ask later, reset the count to 0 giving a grace period of 5 again | |
md.commands.append(new Windows.UI.Popups.UICommand( | |
"Ask Later", | |
function (command) { | |
startedCount = 0; | |
roamingSettings.values["startedCount"] = startedCount; | |
})); | |
// Set the command that will be invoked by default | |
md.defaultCommandIndex = 0; | |
// Set the command to be invoked when escape is pressed | |
md.cancelCommandIndex = 1; | |
// Display the MessageBox asyncronously. | |
md.showAsync(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment