Android User Invite
Overview
Implement and attribute user invite links for when existing users refer others to your app.
For an introduction, see user invite.
Want to see a full example? check out the recipe:
Implement user invites
Before you begin: Sync with the marketer to find out the exact use cases they want for the links and to get a list of the parameters they want to be implemented.
To implement user invite attribution, complete the following steps:
- Set up invite link generation to generate invite links.
- Optional Log the invite link creation.
- Set up Unified Deep Linking (UDL)
- Optional Retrieve referrer data from user invite links.
- Optional Set up referrer rewards.
The following code is based on the marketer example.
Set up invite link generation
To enable users to invite their friends to your app, you need a way to generate user invite links. This is done using LinkGenerator
.
To set up user invite link generation:
-
Make sure to import the following dependencies:
import com.appsflyer.AppsFlyerLib; import com.appsflyer.CreateOneLinkHttpTask; import com.appsflyer.share.LinkGenerator; import com.appsflyer.share.ShareInviteHelper;
-
Set a OneLink template using
setAppInviteOneLink()
(The template ID is provided by the marketer):AppsFlyerLib.getInstance().setAppInviteOneLink("H5hv"); // set the OneLink template ID the user invite links will be based on
Note
- Make sure to call
setAppInviteOneLink()
before callingstart
. - The OneLink template must be related to the app.
- Make sure to call
-
Create a
LinkGenerator
usingShareInviteHelper.generateInviteUrl()
.LinkGenerator linkGenerator = ShareInviteHelper.generateInviteUrl(getApplicationContext());
-
Depending on the user flow you want to achieve, add the following parameters using
linkGenerator.addParameter()
:linkGenerator.addParameter("deep_link_value", <TARGET_VIEW>); linkGenerator.addParameter("deep_link_sub1", <PROMO_CODE>); linkGenerator.addParameter("deep_link_sub2", <REFERRER_ID>); // Optional; makes the referrer ID available in the installs raw-data report linkGenerator.addParameter("af_sub1", <REFERRER_ID>);
deep_link_value
: The app experience the referred user should be deep linked into.deep_link_sub1
: Promo code received by the invitee.deep_link_sub2
: Referrer identifier. Can be used to reward the referrer.- Note: If you have SDK V6.5.2 or lower, you need to encode any parameter values with special characters.
-
Set attribution parameters. (These will display in AppsFlyer dashboards and raw data reports).
linkGenerator.setCampaign("summer_sale"); linkGenerator.setChannel("mobile_share");
-
Optional Set a branded domain for the generated link:
linkGenerator.setBrandDomain("brand.domain.com");
-
Create a
LinkGenerator.ResponseListener
to retrieve the user invite link when it's available:LinkGenerator.ResponseListener listener = new LinkGenerator.ResponseListener() { @Override public void onResponse(String s) { Log.d(LOG_TAG, "Share invite link: " + s); // ... } @Override public void onResponseError(String s) { Log.d(LOG_TAG, "onResponseError called"); } };
Note
Since SDK v6.9.0
LinkGenerator.ResponseListener
replacedCreateOneLinkHttpTask.ResponseListener
onResponse()
is called when the user invite is created successfully.onResponseError()
is called when link generation fails.
- Pass
listener
tolinkGenerator.generateLink()
:linkGenerator.generateLink(getApplicationContext(), listener);
Set the shortlink ID
Optional
The shortlink ID can be determined by the developer, by adding the paramter af_custom_shortlink
to the LinkGenerator
instance.
linkGenerator.addParameter("af_custom_shortlink", <value>);
Log invite link creation events
Optional
To log the invite link creation event:
Log the invite using logInvite()
:
HashMap<String,String> logInviteMap = new HashMap<String,String>();
logInviteMap.put("referrerId", <REFERRER_ID>);
logInviteMap.put("campaign", "summer_sale");
ShareInviteHelper.logInvite(getApplicationContext(), "mobile_share", logInviteMap);
logInvite
results in an af_invite
in-app event.
Note
If you don't want to use a channel, you can use
logEvent
instead.
Set up UDL for user invite attribution
Optional
To set up UDL for user invite attribution:
Set up Unified Deep Linking (UDL). In DeepLinkListener.onDeepLinking()
, retrieve the deep linking parameters created during the link generation step. In this example, the following properties are retrieved:
deep_link_value
, usingDeepLink.getDeepLinkValue()
deep_link_sub1
, usingDeepLink.getStringValue()
deep_link_sub2
, usingDeepLink.getStringValue()
See code: Java.
Reward referrers
Optional
In the following scenarios, User A invites User B to your app.
Reward referrers on install
Scenario: User B installs your app via User A's invite link.
User A's ID is available in DeepLinkListener.onDeepLinking()
and in this example, is retrieved using DeepLink.getStringValue("deep_link_sub2")
. Once you retrieve the ID, add it to the list of referrer IDs to be rewarded. It's up to you how to store and retrieve the list.
Reward referrers for user actions
Scenario: User B makes a purchase. You want to reward User A, who initially referred User B to your app, for the action.
To reward User A for User B's action:
-
Retrieve User A's referrer ID and add it to one of the customizable in-app event parameters (for example,
af_param_1
):Map<String, Object> purchaseEventParameters = new HashMap<String, Object>(); purchaseEventParameters.put(AFInAppEventParameterName.PARAM_1, <REFERRER_ID>); purchaseEventParameters.put(AFInAppEventParameterName.CURRENCY, "USD"); purchaseEventParameters.put(AFInAppEventParameterName.REVENUE, 200); AppsFlyerLib.getInstance().logEvent(getApplicationContext(), purchaseEventParameters);
-
On your backend, retrieve in-app event data
-
Add the found referrer IDs to a list of users to be rewarded.
-
When User A launches the app, check if their referrer ID is on the list of users to be rewarded and reward them if it is.
Note
- Steps 2-3 are not carried out by the mobile developer. Step 4 depends on how steps 2-3 are implemented.
- A purchase event is just an example. This applies to any type of in-app event.
Updated 2 months ago