Skip to content

Instantly share code, notes, and snippets.

@digitarald
Created October 17, 2014 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitarald/7b2e3733abad2ff74d25 to your computer and use it in GitHub Desktop.
Save digitarald/7b2e3733abad2ff74d25 to your computer and use it in GitHub Desktop.
Detect installOrigin in Firefox apps to defer preload status.
/**
* Detect installOrigin in Firefox apps to defer preload status.
*
* For most apps the installOrigin will be https://marketplace.firefox.com
* Preloaded apps *can* customize their installOrigin to reflect country and/or
* carrier (like https://fxos.telefonia.es for Telefónica in Spain).
*
* @param {Function} done Node-style callback (err, origin)
* @example
* detectInstallOrigin(function(err, origin) {
* if (!origin) {
* // Nothing to track
* return;
* }
* // We could override the referrer to the origin in Google analytics
* _gaq.push(['_setReferrerOverride', origin]);
* }
*
* // Or set a custom var on the visitor level
* _gaq.push(['_setCustomVar', 1, 'InstallOrigin', origin, 1]);
*
* // Or redirect to another site
* var href = '/carrier-code?utm_source=' + encodeURIComponent(origin);
* location.href = href;
* });
* @author Harald Kirschner <harald@mozilla.com>
*/
function detectInstallOrigin(done) {
if (!navigator.mozApps) {
done('mozApps not supported');
}
var request = navigator.mozApps.getSelf();
request.onsuccess = function() {
if (request.result) {
done(null, request.result.installOrigin);
} else {
done('App not installed');
}
};
request.onerror = function() {
done('Error: ' + request.error.name);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment