how to add referral code on fetch

Easily Add Referral Codes to Fetch: Step-by-Step Guide

By Laura Smith
AD

Adding a referral code to the Fetch API in JavaScript is a common practice when you want to track where your users are coming from. This could be particularly useful if you're using affiliate marketing or want to analyze your website's traffic sources. Here's a step-by-step guide on how to do this.

Before we dive in, let's understand what the Fetch API is. It's a modern replacement for XMLHttpRequest and is used to make HTTP requests. It's more powerful and flexible, and it works on promises, which makes our code cleaner and easier to read.

Setting Up the Referral Code

First, you need to decide where you want to store the referral code. For this example, let's assume it's in a cookie. If it's not, you can adjust the place where you're getting the referral code from.

AD
How Do I Enter a Referral Code on Fetch?
How Do I Enter a Referral Code on Fetch?
Fetch Referral Code 9EFD5C gives you 4,000 points!
Fetch Referral Code 9EFD5C gives you 4,000 points!
an advertisement with text that reads how does fetch work? when you upload any receipt through the fetch app, you'll be awarded points that can be used to redeem free gift cards
an advertisement with text that reads how does fetch work? when you upload any receipt through the fetch app, you'll be awarded points that can be used to redeem free gift cards

To set up a cookie, you can use the following function. We'll use the 'js.cookie' library, so make sure to include it in your project.

Setting the Cookie

Here's how you can set a cookie with a referral code:

my fav life hack! 🌸🌷🎀
my fav life hack! 🌸🌷🎀
Get 100,000 Points FAST on Fetch Rewards!
Get 100,000 Points FAST on Fetch Rewards!
Fetch rewards! Use my referral code for points (2,000 points)
Fetch rewards! Use my referral code for points (2,000 points)

Cookies.set('referralCode', 'YOUR_REFERRAL_CODE', { expires: 365, path: '/' });

Getting the Cookie

To retrieve the cookie, you can use:

AD

const referralCode = Cookies.get('referralCode');

Join my referral
Join my referral
the first 10 people to use my fetch reward referral code get a shoutout and the first 5 get a follow
the first 10 people to use my fetch reward referral code get a shoutout and the first 5 get a follow
How to Earn Free Gift Cards with the Fetch Rewards App + Fetch Rewards Code
How to Earn Free Gift Cards with the Fetch Rewards App + Fetch Rewards Code
Free Fetch Rewards Referral Code
Free Fetch Rewards Referral Code
do you want free stuff? 100% REAL use my code on fetch rewards! 6QFCUP
do you want free stuff? 100% REAL use my code on fetch rewards! 6QFCUP
Fetch rewards tutorial!
Fetch rewards tutorial!

Incorporating the Referral Code in Fetch

Now that we have the referral code, we can include it in our Fetch API requests. We'll add it as a query parameter in the URL. Here's an example:

First, let's create an async function that will make the request:

AD

async function fetchData(url) {

4 Fun Ways to Refer Friends to Fetch | How To Refer a Friend on Fetch
4 Fun Ways to Refer Friends to Fetch | How To Refer a Friend on Fetch
hey if you download fetch rewards and put in the code JMP2GN as your referral you’ll get loads of po
hey if you download fetch rewards and put in the code JMP2GN as your referral you’ll get loads of po
Use my referral code for points in the Fetch app.
Use my referral code for points in the Fetch app.

const response = await fetch(`${url}?referralCode=${referralCode}`);

const data = await response.json();

AD

return data;

}

Using the Function

Now, you can use this function whenever you're making a request that needs to include the referral code:

fetchData('https://example.com/api/data').then(data => console.log(data));

By including the referral code in your Fetch API requests, you can effectively track where your users are coming from. This data can then be used to analyze your website's traffic, adjust your marketing strategies, and potentially incentivize referrals.

Happy coding! Remember to test your implementation thoroughly to ensure everything works as expected.

AD