Reward referrals
One of the most effective ways to get new users is through user referrals. You can use Dynamic Links along with Realtime Database and Cloud Functions for Firebase to encourage your users to invite their friends by offering in-app rewards for successful referrals to both the referrer and the recipient.
Key benefits
- Accelerate growth by providing an incentive for your users to invite their friends.
- Invitation links work across platforms.
- New users opening your app for the first time get a first-run experience that you customize for them. For example, you can automatically connect them with the friend that invited them.
- Optionally delay granting rewards until new users complete some introductory task, such as completing a tutorial.
Here's how to get started!
Set up Firebase and the Dynamic Links SDK
Set up a new Firebase project and install the Dynamic Links SDK into your app. (iOS, Android, C++, Unity). Installing the Dynamic Links SDK allows Firebase to pass along data about the Dynamic Link to the app, including after the user installs the app. Without the SDK, there's no way to connect a post-install user with a pre-install click.
Create invitation links
To create an invitation, first create the link that the recipient opens to accept the invitation. Later, you will include this link in the text of the invitation. When a recipient of the invitation installs your app by opening the link, they can get a customized first-run experience, including receiving an in-app reward.
This invitation link is a Dynamic Link with a
link
parameter value that indicates it is from your existing user.
There are many ways you can format these
link
parameter payloads and tie them into your app. One simple way is to specify the sender's user account ID in a query parameter like in the following example:https://mygame.example.com/?invitedby=SENDER_UID
Then, to create Dynamic Links suitable for inclusion in an invitation, you can use the Dynamic Link Builder API:
Send the invitations
Now that you have created the link, you can include it in an invitation. The invitation can be an email, SMS message, or any other medium, depending on what is most appropriate for your app and audience.
For example, to send an email invitation:
Retrieve referral information in your app
When the invitation recipient opens the referral link, they will be directed to the App Store or Play Store to install your app if it isn't already installed. Then, when they open your app for the first time, you can retrieve the referral information you included in the Dynamic Link and use it to apply the reward.
Usually, you want to grant referral rewards only after the invitation recipient signs up, or even only after the new user completes some task. Until the reward criteria are met, you need to keep track of the reward information that you got from the Dynamic Link.
One way to keep track of this information is sign in the user anonymously and store the data in the anonymous account's Realtime Database record. When the recipient signs up and the anonymous account is converted to a permanent account, the new account will have the same UID as the anonymous account, and as a result, will have access to the reward information.
For example, to save the referrer's UID after the recipient opens your app:
Then, when the invitation recipient decides to create an account, you can carry over the referral information from the anonymous account to the invitation recipient's new account.
First, get an
AuthCredential
object using the sign-in method the invitee wants to use. For example, to sign-in with an email address and password:
Then, link this credential to the anonymous account:
The new, permanent, account has access to all of the reward data you added to the anonymous account.
Grant rewards to the referrer and the recipient
Now that you have retrieved and saved the invitation data from the Dynamic Link, you can grant the referral rewards to the referrer and the recipient whenever the criteria you want to require have been met.
Although you can write to the Realtime Database from your client app, you will often want to allow only read access to data like in-app currency from your apps, and perform write operations only from your backend. This backend could be any system capable of running the Firebase Admin SDK, but it is often easiest to use Cloud Functions to perform these tasks.
For example, suppose you have a game and you want to grant a reward of in-game currency to the recipient after the recipient signs up, and to the referrer after the recipient reaches level 5.
To grant the reward for signing up, deploy a function that watches for a specific Realtime Database key to be created, and grants the reward when it is. For example:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
.onCreate(event => {
var uid = event.params.uid;
admin.database().ref(`users/${uid}/referred_by`)
.once('value').then(function(data) {
var referred_by_somebody = data.val();
if (referred_by_somebody) {
var moneyRef = admin.database()
.ref(`/users/${uid}/inventory/pieces_of_eight`);
moneyRef.transaction(function (current_value) {
return (current_value || 0) + 50;
});
}
});
});
Then, when a new user signs up, trigger this function by creating the Realtime Database key. For example, trigger the function in
linkWithCredential
's success listener, which you created in the previous step:
To grant a reward to the referrer when the recipient reaches level 5, deploy a function that watches for changes to the
level
field in your user records. If a user went from level 4 to level 5, and the user has a referrer recorded, grant the reward:const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.rewardReferrals = functions.database.ref('/users/{uid}/level')
.onUpdate(event => {
var level = event.data.val();
var prev_level = event.data.previous.val();
if (prev_level == 4 && level == 5) {
var referrerRef = event.data.ref.parent.child('referred_by');
return referrerRef.once('value').then(function(data) {
var referrerUid = data.val();
if (referrerUid) {
var moneyRef = admin.database()
.ref(`/users/${referrerUid}/inventory/pieces_of_eight`);
return moneyRef.transaction(function (current_value) {
return (current_value || 0) + 50;
});
}
});
}
});
Both the referrer and your new user have now received their rewards.
Comments
Post a Comment