Skip to content

Commit c0f07ad

Browse files
committed
rework for clarity, add sections
1 parent 43381bf commit c0f07ad

File tree

1 file changed

+70
-17
lines changed

1 file changed

+70
-17
lines changed

docs/activities/Development_Guides.mdx

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -887,34 +887,87 @@ This example is being done entirely on the client, however, a more common patter
887887
---
888888
889889
### Prompting Users to Share Incentivized Links
890-
Virality is a valuable way to grow your userbase and your application. In order to leverage network effects, we suggest using our SDK's `shareLink` command to prompt users to share so-called "incentivized links" and receive rewards for sharing.
891890
892-
An example reward would be something like free coins (or any other in-game resource) for users who click the link and for those who shared the link.
891+
Incentivized sharing can help grow your application through network effects. This guide covers implementing a reward system for users who share links and those who click them.
892+
893+
#### Implementation Overview
894+
1. Create and track promotional campaigns
895+
2. Prompt users to share links
896+
3. Handle incoming referrals
897+
4. Distribute rewards safely
898+
899+
#### Sharing Links
900+
901+
When implementing sharing, you'll need to:
902+
1. Generate a unique ID for tracking the promotion
903+
2. Call the `shareLink` command
904+
3. Track the share attempt
893905
894-
To share the link, use `shareLink`.
895906
```javascript
896-
// custom ID creation, persistence, and resolution is up to you as a developer
907+
// Generate a unique ID for this promotion
908+
// This could be per-campaign, per-user, or per-share depending on your needs
897909
const customId = await createPromotionalCustomId();
898910

899-
const { success } = await DiscordRPC.commands.shareLink({
900-
message: 'Click this link to redeem 5 free coins!',
901-
custom_id: customId,
902-
});
903-
success ? console.log('User shared link!') : console.log('User did not share link!');
911+
try {
912+
const { success } = await DiscordRPC.commands.shareLink({
913+
message: 'Click this link to redeem 5 free coins!',
914+
custom_id: customId,
915+
// referrer_id is optional - if omitted, the current user's ID is used
916+
});
917+
918+
if (success) {
919+
// Track successful share for analytics/limiting
920+
await trackSuccessfulShare(customId);
921+
}
922+
} catch (error) {
923+
// Handle share failures appropriately
924+
console.error('Failed to share link:', error);
925+
}
904926
```
905-
Calling `shareLink` will prompt the user with a modal to share the link. The `success` returned indicates if the link was shared or copied. Note that we did not add a `referrer_id` in this example, as the user ID of the user sharing the link will automatically be appended as a referrer if left empty.
906927
907-
So now that the initial user has shared a link, what happens when their friend receives the message and clicks the link?
908-
Clicking the link (or Play from the Embed) will launch the activity and pass the `customId` and `referredId` to the SDK. Once launched, the SDK will expose the `customId` and `refererId` and you can resolve the IDs and grant the rewards as promised.
928+
#### Handling Incoming Referrals
929+
When a user clicks a shared link, your activity will launch with referral data available through the SDK:
909930
910931
```javascript
911-
// for illustrative purposes -- this must be implemented by you as a developer
912-
await resolvePromotion({
913-
customId: DiscordRPC.customId, // your promotional ID, which corresponds to granting reward (coins)
914-
refererId: DiscordRPC.referredId, // the user ID of the initial user who shared the link
915-
});
932+
// Early in your activity's initialization
933+
async function handleReferral() {
934+
// Validate the referral data
935+
if (!DiscordRPC.customId || !DiscordRPC.refererId) {
936+
return;
937+
}
938+
939+
try {
940+
// Verify this is a valid promotion and hasn't expired
941+
const promotion = await validatePromotion(DiscordRPC.customId);
942+
if (!promotion) {
943+
console.log('Invalid or expired promotion');
944+
return;
945+
}
946+
947+
// Prevent self-referrals
948+
if (DiscordRPC.refererId === currentUserId) {
949+
console.log('Self-referrals not allowed');
950+
return;
951+
}
952+
953+
// Grant rewards to both users
954+
await grantRewards({
955+
promotionId: DiscordRPC.customId,
956+
refererId: DiscordRPC.refererId,
957+
newUserId: currentUserId
958+
});
959+
} catch (error) {
960+
console.error('Failed to process referral:', error);
961+
}
962+
}
916963
```
917964
965+
#### Link Sharing Best Practices
966+
- Generate unique, non-guessable `customId`s
967+
- Track and validate referrals to prevent abuse
968+
- Handle edge cases like expired promotions gracefully
969+
- Consider implementing cool-down periods between shares
970+
918971
### Preventing unwanted activity sessions
919972
920973
Activities are surfaced through iframes in the Discord app. The activity website itself is publicly reachable at `<application_id>.discordsays.com`. Activities will expect to be able to communicate with Discord's web or mobile client via the Discord SDK's RPC protocol. If a user loads the activity's website in a normal browser, the Discord RPC server will not be present, and the activity will likely fail in some way.

0 commit comments

Comments
 (0)