Skip to content
Darren Clark edited this page Aug 29, 2014 · 8 revisions

Tracking Currency

Tracking in-game 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, it's very important you read the For Existing Apps section.

Registering Currencies

You'll need to let Yerdy know which currencies you are tracking via the Yerdy.Init() method you call on application startup. For example:

// Currency 1 -> Gold
// Currency 2 -> Silver
// Currency 3 -> Bronze
Yerdy.Init("<your publisher key>", "Gold", "Silver", "Bronze");

Note: The order of the currencies is important. You MUST NOT reorder them ever. 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 call to: Yerdy.Init("<your publisher key>", "Gold", "Silver", "Bronze", "Rubies");

After registering your currencies in code, you'll also need to let the dashboard know which currencies are which. Navigate to your app on the Apps page, then ConfigureGame Currencies.

Reporting In-Game Currency Transactions

Yerdy supports three types of transactions:

  • User earned currency (Yerdy.EarnedCurrency(...))
  • User purchased an in game item (Yerdy.PurchasedItem(...))
  • User made an in-app purchase (Yerdy.PurchasedInApp(...))

All three of these methods have overloads that take dictionary mapping currency names to their amounts. For example:

// 5 gold
new Dictionary<string, uint>()
{
	{ "Gold", 5 }
};

// 10 gold, 5 silver
new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "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 Yerdy.EarnedCurrency(...) methods:

Yerdy.EarnedCurrency("Gold", 10);

// or, if they earned multiple currencies
var currencies = new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "Silver", 5 },
};
Yerdy.EarnedCurrency(currencies);

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 Yerdy.PurchasedItem(...) methods:

string itemName = "Superbooster";

Yerdy.PurchasedItem(itemName, "Gold", 10);

// or, if they bought the item with multiple currencies
var currencies = new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "Silver", 5 },
};
Yerdy.PurchasedItem(itemName, currencies);

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 Yerdy.PurchasedInApp(...) methods. Depending on what platform you are running on, the data you pass in will be a little different.

iOS

To report a purchase on iOS you'll need to create a YerdyPurchaseiOS with the product identifier (and, if you have it, the transaction identifier) and then pass that into one of the Yerdy.PurchasedInApp(...) methods:

string productIdentifier = ...; // product identifier
var purchase = new YerdyPurchaseiOS(productIdentifier);

// if your IAP plugin provides you with the transactionIdentifier as well, you should use:
// 	new YerdyPurchaseiOS(productIdentifier, transactionIdentifier)

Yerdy.PurchasedInApp(purchase, "Gold", 10);

// or, if the IAP was for multiple currencies
var currencies = new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "Silver", 5 },
};
Yerdy.PurchasedInApp(purchase, currencies);

Android

To report a purchase for Android you'll need to create a YerdyPurchaseGoogle or YerdyPurchaseAmazon object with the product identifier and additional information depending on store platform. Then pass that into one of the Yerdy.PurchasedInApp(...) methods:

Google

For Google purchases the Google receipt and signature are both required from the transaction.

string productIdentifier = ...; // product identifier
string purchaseValue = ...; // Product real currency value ex. "$0.99"
string purchaseReceipt = ...; // Purchase receipt from Google
string purchaseSignature = ...; // Purchase signature from Google
bool sandboxPurchase = false; // Can manually set purchases to be tracked as test purchases
var purchase = new YerdyPurchaseGoogle(productIdentifier, purchaseValue, purchaseReceipt, purchaseSignature, sandboxPurchase);

Yerdy.PurchasedInApp(purchase, "Gold", 10);
	
// or, if the IAP was for multiple currencies
var currencies = new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "Silver", 5 },
};
Yerdy.PurchasedInApp(purchase, currencies);
Amazon

For Amazon purchases the Amazon receipt and user are both required from the transaction.

string productIdentifier = ...; // product identifier
string purchaseValue = ...; // Product real currency value ex. "$0.99"
string purchaseReceipt = ...; // Purchase receipt from Amazon
string amazonUser = ...; // Amazon current user
bool sandboxPurchase = false; // Can manually set purchases to be tracked as test purchases
var purchase = new YerdyPurchaseAmazon(productIdentifier, purchaseValue, purchaseReceipt, amazonUser, sandboxPurchase);

Yerdy.PurchasedInApp(purchase, "Gold", 10);
	
// or, if the IAP was for multiple currencies
var currencies = new Dictionary<string, uint>()
{
	{ "Gold", 10 },
	{ "Silver", 5 },
};
Yerdy.PurchasedInApp(purchase, currencies);

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

For Existing Apps

If you are integrating Yerdy after you have released your app to an app store, you need to do a little extra work to ensure existing currency is tracked properly. For existing users, you need to call Yerdy.SetUserAsPreYerdy(...) with their current balance. For example:

void Start()
{	
	// check if the "Gold" PlayerPrefs key exists to see if this is a new install or not
	bool isNewInstall = PlayerPrefs.HasKey("Gold");
	
	if (!PlayerPrefs.HasKey("SetupYerdyUser")) {
		if (!isNewInstall) {
			// user has existing currency but it hasn't been reported to Yerdy
    		int gold = PlayerPrefs.GetInt("Gold");
    		var currency = new Dictionary<string, uint>()
    		{
    			{ "Gold", (uint)gold },
    		};
			Yerdy.SetUserAsPreYerdy(currency);
		}
		
    	// set this PlayerPref so that we don't run the above code again
		PlayerPrefs.SetInt("SetupYerdyUser", 1);
	}
}