Skip to content

Instantly share code, notes, and snippets.

@FokkeZB
Last active December 16, 2015 16:49
Show Gist options
  • Select an option

  • Save FokkeZB/5466012 to your computer and use it in GitHub Desktop.

Select an option

Save FokkeZB/5466012 to your computer and use it in GitHub Desktop.
Rate-my-app CommonJS module for Titanium
var rate = require('rate');
// Set texts
rate.title = 'Pleeeeease rate!';
rate.message = 'I would be so thankful!';
rate.yes = 'Fine';
rate.later = 'Maybe later';
rate.never = 'Forget it';
// Set triggers
rate.pointsBetween = 100; // Reach 100 points before asking and between each repeat
rate.daysBetween = 10; // Wait 10 days before asking and between each repeat
// Set iTunes Track ID manually
rate.iTunesTrackId = 123456;
// Reset counts (WARNING: Will also reset the user's wish to never be asked again!)
rate.reset();
// Add 1 point and test if we should ask
rate.plus();
// Add more points and do not test
rate.plus(5, false);
// Just test
rate.test();
// Just ask
rate.show();
var _properties = {}, _platform;
function plus(_points, _show) {
if (typeof _points !== 'number') {
_points = 1;
}
return _trigger(_points, _show);
}
function test() {
return _trigger();
}
function ask() {
if (typeof _platform !== 'string') {
_platform = Ti.Platform.name;
}
if (_platform === 'iPhone OS' && !exports.iTunesTrackId) {
_lookup(function (result) {
if (!result) {
Ti.API.debug('[RATE] Lookup failed.');
return;
}
exports.iTunesTrackId = result.trackId;
_ask();
return;
});
return;
}
var buttonNames = [exports.yes, exports.later, exports.never];
var cancel = buttonNames.length - 1;
var alertDialog = Titanium.UI.createAlertDialog({
title: exports.title,
message: exports.message,
buttonNames: buttonNames,
cancel: cancel
});
alertDialog.addEventListener('click', function(e) {
if (buttonNames[e.index] === exports.yes) {
Ti.App.Properties.setBool('rate_done', true);
show();
} else if (buttonNames[e.index] === exports.never) {
Ti.App.Properties.setBool('rate_done', true);
}
return;
});
alertDialog.show();
return;
}
function open() {
if (Ti.Platform.name === 'android') {
Ti.Platform.openURL('market://details?id=' + Ti.App.id);
} else if (Ti.Platform.name === 'iPhone OS') {
if (!exports.iTunesTrackId) {
_lookup(function (result) {
if (!result) {
Ti.API.debug('[RATE] Lookup failed.');
return;
}
exports.iTunesTrackId = result.trackId;
return open();
});
return;
}
Ti.Platform.openURL('itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=' + exports.iTunesTrackId + '&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software');
}
return;
}
function reset() {
Ti.API.debug('[RATE] Reset.');
Ti.App.Properties.removeProperty('rate_done');
Ti.App.Properties.removeProperty('rate_points');
Ti.App.Properties.removeProperty('rate_asked');
return;
}
function _trigger(_points, _show) {
if (Ti.App.Properties.getBool('rate_done', false) === true) {
Ti.API.debug('[RATE] Rating already done or rejected.');
return;
}
var points = Ti.App.Properties.getInt('rate_points', 0);
if (_points) {
points = points + (_points || 1);
Ti.API.debug('[RATE] Rating points changed to: ' + points);
}
var now = (new Date() / 1000),
checked = Ti.App.Properties.getInt('rate_asked', 0);
if (_show !== false && points >= exports.pointsBetween && (now - checked) >= (exports.daysBetween * 86400)) {
Ti.API.debug('[RATE] Rating triggered!');
Ti.App.Properties.setInt('rate_points', 0);
Ti.App.Properties.setInt('rate_asked', now);
ask();
} else {
Ti.App.Properties.setInt('rate_points', points);
}
return;
}
function _lookup(_callback) {
var xhr = Ti.Network.createHTTPClient({
onload: function (e) {
if (xhr.status === 200 && this.responseText) {
try {
var json = JSON.parse(this.responseText);
if (json.resultCount === 1) {
_callback(json.results[0]);
return;
} else {
Ti.API.error('[XHR:response] ' + this.responseText);
}
} catch (err) {
Ti.API.error('[XHR:json] ' + JSON.stringify(err));
}
}
_callback();
return;
},
onerror: function (e) {
Ti.API.error('[XHR:error] ' + JSON.stringify(e.error));
_callback();
return;
}
});
var url = 'http://itunes.apple.com/lookup?';
if (exports.iTunesTrackId) {
url = url + 'id=' + exports.iTunesTrackId;
} else {
url = url + 'bundleId=' + Ti.App.id;
}
xhr.open('GET', url);
xhr.send();
return;
}
exports.title = 'Like our app?';
exports.message = 'Please take a minute to rate it:';
exports.yes = 'Yes, of course';
exports.later = 'Ask me later';
exports.never = 'No, thank you';
exports.iTunesTrackId = null;
exports.daysBetween = 3;
exports.pointsBetween = 3;
exports.plus = plus;
exports.test = test;
exports.ask = ask;
exports.open = open;
exports.reset = reset;

I wanted a intelligent library that pops the "Rate my app" question at the right time and is easy to set-up.

Well, this library at minimum requires no configuration at all and takes just a single line of code, because it reads your app's (bundle) ID from Ti.App.id and looks up the App Store ID itself:

require('rate').plus();

Check out advanced.js for all available options.

I personally like to add points at places where the user has positive interaction with the app, but wait to pop the question until he/she is finished doing that.

require('rate').plus();
@rborn
Copy link

rborn commented May 6, 2013

One advice to those using this module ( not a technical issue):

Take GOOD CARE what you are putting there in the title and message, Apple is very picky related to this, don't ask the user to give a good rating - they consider that you are braking the point 3.10 in the guidelines.

I know this as we got a rejection with ArtistBox because of this:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment