Tracking Currency - YerdySDK/yerdy-android GitHub Wiki

Tracking Currency

Tracking currency in Yerdy requires 4 simple steps:

  1. Registering Currencies
  2. Reporting when users earn currency
  3. Reporting when users buy in-game items
  4. Reporting when users buy an in-app purchase

If you are integrating Yerdy into an existing app that has been released to the Google Play Store or Amazon App Store, it's very important you read the For Existing Apps section.

Registering Currencies

You can register up to 6 currencies via the configureCurrencies(Context context, String[] names) method.

// Currency 1 -> Gold
// Currency 2 -> Silver
// Currency 3 -> Bronze
Yerdy.getInstance().configureCurrencies(this, new String[] { "Gold", "Silver", "Bronze"});

Note: The order of the currencies is important. You MUST NOT reorder them. However, you can append new currencies. For example, if we add a new currency to our game in a later release, we would update the array to: { "Gold", "Silver", "Bronze", "Rubies" }

Reporting In-Game Currency Transactions

Yerdy supports three types of transactions:

  • User earned currency earnedCurrency(Context context, Map<String, Integer> currencies)
  • User purchased an in game item purchasedItem(String item, Map<String, Integer> currencies)
  • User made an in-app purchase purchasedInApp(YRDPurchase purchase, Map<String, Integer> currencies)

All three of these methods take a Map<String,Integer> mapping currency names to their amounts. For example:

// 5 gold
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Gold", 5);

// 10 Gold, 5 Silver
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Gold", 10);
map.put("Silver", 5);

Note: It's important you use the exact same currency names you registered earlier.

User earned currency

When a user earns currency in your game (for example, when they complete a mission) you should report that to Yerdy via the earnedCurrency(Context context, Map<String, Integer> currencies) method:

Map<String, Integer> earnedCurrencies = new HashMap<String, Integer>();
earnedCurrencies.put("Gold", 10);
earnedCurrencies.put("Silver", 5);
Yerdy.getInstance().earnedCurrency(this, earnedCurrencies);

Note: All earned currency amounts must be positive values (greater than 0).

User purchased an in-game item

When a user purchases an in-game item (for example, when they buy a powerup with your in-game currency) you should report that to Yerdy via the purchasedItem(String item, Map<String, Integer> currencies) method:

String itemName = "Superboost";
Map<String, Integer> itemPrice = new HashMap<String, Integer>();
itemPrice.put("Silver", 5);
Yerdy.getInstance().purchasedItem(itemName, itemPrice);

Note: All spent currency amounts must be positive values (greater than 0).

User made an in-app purchase

When a user purchases an in-app purchase you should report that to Yerdy via the purchasedInApp(YRDPurchase purchase, Map<String, Integer> currencies) method.:

Map<String, Integer> earnedCurrencies = new HashMap<String, Integer>();
earnedCurrencies.put("Gold", 5);
earnedCurrencies.put("Silver", 10);
earnedCurrencies.put("Bronze", 15);
Yerdy.getInstance().earnedCurrency(this, earnedCurrencies);
Yerdy.getInstance().purchasedInApp([YRDPurchaseGoogle/YRDPurchaseAmazon], map);

Google

YRDPurchaseGoogle purchase = new YRDPurchaseGoogle("com.your.item.sku", [Real Currency Value], [Google Purchase Receipt], [Google Purchase Signature], [Test Purchase]);

Amazon

YRDPurchaseAmazon purchase = new YRDPurchaseAmazon("com.your.item.sku", [Real Currency Value], [Amazon Purchase Receipt], [Amazon User Name], [Test Purchase]);

If you have any in-app purchases that don't reward currency (for example, a "Remove Ads" product), you can pass in null for the currencies or you can use the purchasedInApp(YRDPurchase purchase) method.

Note: All purchased currency amounts must be positive values (greater than 0).

For Existing Apps

If you are integrating Yerdy after you have released, you need to do a little extra work to ensure existing currency is tracked properly. For existing users, you need to call setExistingCurrenciesForPreYerdyUser(Map<String, Integer> currencies) with their current balance. For example:

// Yerdy setup, currency registration, etc...

boolean reportedUserToYerdy = ...; // load from disk/preferences
if(!reportedUserToYerdy) {
	Object userCurrencies = ...; // load user's currency from disk or wherever they are stored
	// if user has currencies, it means they have played before
	if (userCurrencies != null) {
		Map<String, Integer> currencies = new HashMap<String, Integer>();
		currencies.put("Gold", userCurrencies.Gold);
		Yerdy.getInstance().setExistingCurrenciesForPreYerdyUser(currencies);
	}
	
	// this should be written to disk/preferences to prevent the above code running again
	reportedUserToYerdy = true;
}