Let's imagine that you want to reward a user with a free trial day if they use a feature in your product for uploading a file. This allows the user to get acquainted with your product and you can reward them for the interaction.
You can accomplish this by triggering a loyalty action for your user. The example below is what that code could look like when using NodeJS.
Code Example
upload-file.js
constrequest=require("request");constoptions= { method:'POST', url:'https://api.loyaltysurf.io/v1/campaign/4pdlhb/loyalty-action',// Replace '4pdlhb' with your LoyaltySurf campaign ID headers: { Authorization:'Bearer <YOUR_API_KEY>'// Replace '<YOUR_API_KEY>' with your API key -- get this from https://app.loyaltysurf.io/settings }, json:true};constuploadFile= () => {// ...code that allows the user to upload a file...options.body = { participantEmail:'gavin@hooli.com',// Replace 'gavin@hooli.com' with your user's email address rewardId:'jw83va',// Replace 'jw83va' with a reward ID from your LoyaltySurf campaign };// Send the API request to trigger the loyalty actionrequest(options, (error, response, body) => {if (error) {thrownewError(error); }// Check to see if the loyalty action was triggered successfullyconst { success,message } =body.success;if (success) {console.log('loyalty action trigger success!'); } else {console.log('loyalty action trigger failed :( ', message); } });};
At line 5, replace 4pdlhb with your LoyaltySurf campaign ID. This can be found within the browser's URL bar of any webpage that you are viewing your LoyaltySurf campaign from.
At line 7, replace <YOUR_API_KEY> with your API key, which you can get from your account settings page.
At line 16, set participantEmail with your user's email address (the person who just completed the loyalty action).
At line 17, set rewardId with the Reward ID that you want to trigger a loyalty action for. You can find this value in the Rewards tab or the Installation tab of the LoyaltySurf campaign editor.
Once you trigger a loyalty action, your user will get added to your LoyaltySurf campaign if they do not already exist. Any future loyalty actions that are triggered by the same email address will increment the existing participant's loyalty action count.
Set up reward fulfillment
Once the loyalty action is triggered, and if the campaign reward's loyalty action conversion threshold is 1, then a reward will be generated for the participant. You can set up Webhooks to automate the reward of giving out the free trial day.
With the REST API, you can retrieve a participant's LoyaltySurf rewards or loyalty action count to display on your website or mobile app using the GETParticipant by email or GETParticipant by ID endpoints.
The example below is what that code could look like in NodeJS.
Code Example
signup.js
constrequest=require("request");let options = { method:'GET', url:'https://api.loyaltysurf.io/v1/campaign/4pdlhb/participant/gavin@hooli.com',// Replace '4pdlhb' with your LoyaltySurf campaign ID and replace 'gavin@hooli.com' with your user's email address headers: { Authorization:'Bearer <YOUR_API_KEY>'// Replace '<YOUR_API_KEY>' with your API key -- get this from https://app.loyaltysurf.io/settings }, json:true};constgetUserDetails= () => {// ...code that gets your users' details...// Get the LoyaltySurf participant's detailsrequest(options, (error, response, body) => {if (error) {thrownewError(error); }// Output the response that is returnedconsole.log(body); });};
At line 5:
Replace 4pdlhb with your LoyaltySurf campaign ID. This can be found within the browser's URL bar of any webpage that you are viewing your LoyaltySurf campaign from.
Replace gavin@hooli.com with your user's email address (the person you want to retrieve loyalty program details for).
At line 7, replace <YOUR_API_KEY> with your API key, which you can get from your account settings page.
At line 21, the console.log(body) will output a response that looks like the below example: