DocumentationApplication Username

Application Username with Cordova

Overview

Iaptic stores purchases information and associates them with your customers. For this, we need the application username.

Let's see how to associate the application username with a receipt when using a cordova-based framework.

plugin version >= 9

Starting with version 9 of the plugin, you should set store.applicationUsername as soon as your user is logged in. The information will be passed along with all calls to the receipt validator.

store.applicationUsername = function() {
  return Session.getUserID(); // a string
}

If login is mandatory to activate purchases, the recommended pattern is to wait for the session to be ready before initializing the plugin. This way, applicationUsername will be set with all your validation requests.

SessionService.addEventListener("ready", initIAP);
function initIAP() {
  SessionService.removeEventListener("ready", initIAP); // only initialize the plugin once
  // initialize the plugin
  store.register(products);
  // (your event handlers)
  store.when("product")
    .approved(p => p.verify())
    .verified(p => p.finish());
  store.applicationUsername = () => SessionService.getUserID();
  store.refresh();
}

Check the official documentation for details.

plugin version < 9

With older version of the plugin, you had to add it as an additional argument when making a purchase with store.order().

store.order('my_product_id', {
    applicationUsername: 'application_user_id'});

You should then start seeing users identified by your applicationUsername.

This method might miss some purchases, in particular if they were initiated outside the app. That’s why the API changed, so we recommend you use the most up-to-date version of the plugin.

Good practice & recommendations

Deployment ID

It's highly recommended to prefix the applicationUsername you use with a deployment identifier. For example:

store.applicationUsername = (prod ? "prod" : "dev") + ":" + userID;

This way there will no intersection between production and development data. You can also extend this when more deployments are in place (staging, beta, etc.)

Anonymous IDs

Make sure you send an anonymous user identifier, using the email or another identifier that we could associate with a real person goes against our privacy policy.

Good examples of anonymous identifiers used for applicationUsername:

  • a random number or string used as an identifier in your database.
  • an email address transformed with a hashing function.
  • a random token that you store in your users database.

See also