Created
October 17, 2014 18:06
-
-
Save digitarald/7b2e3733abad2ff74d25 to your computer and use it in GitHub Desktop.
Detect installOrigin in Firefox apps to defer preload status.
This file contains 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
/** | |
* 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