Tumgik
shaheerzeb · 3 years
Text
Laravel: How to Create a Referral System
Today we will see how to define a Referral System in Laravel.
The features that we are going to implement are:
   - Each user will have a code (URL) to get referrals     -Each user will be able to see their list of referrals
What is a referral system?
An application is said to include a referral system when its users can invite others. Link to get referrals
Why would one user go to the trouble of inviting others?
Rewards are generally awarded for each registration achieved, or for each action taken by users who have been referred.
For example:
  - If you invite a person and this person pays for a membership, you get a free month.   -  If you invite a person and this person makes a purchase, you get a% of the purchase value as a thank you.   -  If you get 10 people to sign up as your referrals, you get $ 100 as a thank you.
Actually, there are many ways to reward a user who invites others to join.
Inclusive:
   Some apps provide volume benefits,    or they allow you to manage many levels of referrals (where you not only get benefits from your direct referrals; also from those referred by them).
In our example today we are going to lay the foundations for a referral system.
The benefits to be granted to users is a theme that you can implement, depending on the needs of your application or project.
And if you need help, remember that the advisory service could be convenient for you.
Referral link
To keep the identity of the site safe I replaced the domain.
But the idea is that:
Provide users with a URL that allows them to get referrals.
This URL is also known as the "Affiliate URL".
So our first step will be to define a route that respects this format.
We achieve that with this line:
Route::get('/r/{referralCode}', 'ReferralController@link')->name('referral.link');
Note
In this case, a URL is enough to get referrals. However, there are other ways to put this.     For example, instead of having a referralCode route parameter, an alternative is to have a GET ref parameter that activates the logic.     If a GET parameter is used, then it is possible to obtain referrals not only from a specific route, but from any route in our application.     The latter can be achieved by making use of a ReferralMiddleware.
This time we are going to keep things simple.
Remember that this is the base and that you can continue adding features according to your needs.
Code to get referrals
The idea is that each user has their own code to attract referrals.
For this you can go to the User model and define a static method that generates a random code (of 7 characters in this case).
The do while allows us to continue generating codes in case the result is already in use:
public static function getUniqueReferralCode () {    do {        $ code = Str :: random (7);    } while (User :: where ('referral_code', $ code) -> exists ());
   return $ code; }
To generate the random string we are using the random method of the Laravel Str class.
And this time we are considering a referral_code column in our users table. Don't forget to add this column in your migrations.
If you are using the authentication system that Laravel incorporates, then in your RegisterController you will have something similar to the following:
protected function create (array $ data) {    return User :: create ([        'name' => $ data ['name'],        'email' => $ data ['email'],        'password' => Hash :: make ($ data ['password']),        'role' => $ data ['role'],        'referral_code' => User :: getUniqueReferralCode ()    ]); }
And don't worry about weird characters:
The random method assigns a number or a letter, which can be uppercase or lowercase.
In my case, I have Factory and Seeder classes associated with the User model, so I have easily generated thousands of records to confirm this: Test users with their corresponding referral code
You also don't have to worry that there aren't enough unique codes for all users.
   Let's say we have 24 letters. Considering upper and lower case we would have 48 possible values.    Since numbers also appear, from 0 to 9 we have 10 more possible values.    This means that for each position we have 58 possible values.    In just 3 positions it is possible to have 58x58x58 unique codes. In other words, there is supply for 195 thousand 112 users.    Imagine how many combinations are possible with 7 positions, if with 4 positions we already have more than 11 million combinations.
Rules to attract referrals
Imagine the following scenario:
   I share with you my affiliate link. You are interested in the service or product.    Then someone else finds out about your interest and shares their corresponding link with you.    You register and you become a regular user of the platform.    Who should be rewarded for inviting you to sign up?
The fairest thing is that you are referred by the first user who invited you.
Even if you do not register on the same day, but do so within the next 7 days, it would be convenient for the system to consider you as a referral by the person who initially invited you.
And if you don't register in 7 days and you do it after a long time:
   Then the system would consider you as referred by another person (depending on who shared their link with you first, after 7 days).    And if it was nobody, then the referred_by field would be empty.
These are the rules to consider for the project I am developing.
You can guide yourself from the example that we are seeing and implement your referral system with the rules that you consider most convenient.
In my users table I have the following fields defined:
$ table-> string ('referral_code', 7) -> unique ();
$ table-> unsignedBigInteger ('referred_by') -> nullable (); $ table-> foreign ('referred_by') -> references ('id') -> on ('users');
A Cookie to remember who invited you
Having our database ready, it is time to write the logic for attracting referrals.
We start by creating our controller:
php artisan make: controller ReferralController
And then the method that will serve visits to the invitation URLs:
public function link (Request $ request, $ referralCode) {    if (! $ request-> hasCookie ('referral')) {        $ cookie = cookie ('referral', $ referralCode, 60 * 24 * 7);
       return redirect ('/') -> withCookie ($ cookie);    }
   return redirect ('/'); }
This method indicates that:
   If there is no cookie called referral, we create one and redirect the user to the root path of our application.    And if one already exists, we simply redirect the user without taking any special action.
The cookie helper receives 3 parameters, in this order: the name of the cookie, the value the cookie will have, and the duration in minutes. Consuming the Cookie
Or should I say "eating the cookie"?
What I'm going to do is that, if we already have a Cookie with the "referral_code" of the user who made the invitation, the following is:
Read the value of the Cookie during registration.
In our create method of RegisterController now we will have:
return User :: create ([    'name' => $ data ['name'],    'email' => $ data ['email'],    'password' => Hash :: make ($ data ['password']),    'role' => $ data ['role'],    'referral_code' => User :: getUniqueReferralCode (),    'referred_by' => $ this-
0 notes
shaheerzeb · 3 years
Text
How Amazon’s radar-based sleep tracking could work
0On July 9, Amazon received approval from the Federal Communications Commission to create a radar device that could monitor your sleep health.
The company states that “it plans to use the radar’s capability of capturing motion in a three-dimensional space to enable contactless sleep tracing functionalities.”
If this sounds familiar, Google deployed a similar sleep-tracking radar system called Soli through its Nest Hub device earlier this year.
Radar represents an interesting method for tracking sleep: it’s different from how a camera works (which naturally raises privacy concerns if it’s in your bedroom) and it means the sleeper doesn’t have to actually wear anything to track their slumber. Other sleep-monitoring devices include wearables like a Fitbit (also owned by Google), an Apple Watch, or an under-the-mattress sensor, like this one from Withings.
[Related: Skip the wearables and track your sleep with these 5 apps]
“What radar does, when you think about aircraft radar, is it’s tracking the direction and velocity of things,” says Chris Harrison, an associate professor of Human-Computer Interaction at Carnegie Mellon University. “Radar’s been around since WWII, and we’ve been able to miniaturize it and reduce the cost such that you can stick it into a smartphone or a bedside appliance.”
Tumblr media
Researchers have been able to use these miniaturized radar sensors to detect multiple individuals, and differentiate between different members of the household and even family pets, Harrison explains. Different research groups have shown that radar sensors can pick up respiration and heart rate, in addition to movement. He imagines that Amazon’s sleep tech will be able to gauge the expansion and contraction of your lungs and the blankets to be able to see how you’re breathing in bed. And if you toss and turn, that has a very distinctive radar signal that can be picked up.
Harrison welcomes this potential development. “Number one, I think we all need to sleep better,” he says. “Number two, what I really like about this approach is that it’s really innately privacy preserving. No one really wants a camera in their bedroom.”
That said, a technology like this does have potential concerns that accompany it. “Even though it’s anonymized, and I can’t recognize your radar data over mine, that doesn’t mean it can’t contain very damaging information.”
One way for Amazon to handle privacy concerns would be to not send the data to the cloud. “Do we really want Amazon to know that there were two heartbeats, or one heartbeat, or I had a restless sleep so I wake up in the morning and suddenly all I get on Amazon is sleeping pills they’re advertising to me because they know I had a bad night’s sleep,” he wonders. “I think there’s potential for abuse. As consumers, we should be wary that that’s a possibility and that we want to make sure that we do read the fine print a little more closely and hold companies to an ethical standard.” (Amazon did not respond to requests for comment.)
Google announced that its sleep-tracking tech would process all data in the Nest device.
Based on Amazon’s communications with the FCC, what we know about the new tech so far is that, similar to Google, Amazon’s “Radar Sensors” will be operated through “touchless control,” which lets users interact with devices without direct contact. Instead, radar pulses that are sent out can sense basic gestures you make in the air and translate them into commands such as replaying a song or turning down the volume. The device will also be “non-mobile,” and can be used only when it’s plugged into a power source.
Amazon has been eyeing a way to break into sleep health since August 2020, when it announced that its Amazon Halo band would use AI-powered health tools to help users monitor sleep. This new foray comes at a time when Americans should be getting more shut-eye: Approximately 35 percent of adults in the US get less than the recommended seven hours of sleep per night, according to the Center for Disease Control and Prevention. Losing sleep regularly may negatively impact emotional and physical health. (And of course, you don’t need tech, or monitoring equipment, to get a good night’s sleep.)
[Related: Why do brains need sleep? These ‘twinkling’ star-shaped cells may help us find out]
As for which type of tech is best for analyzing sleep health, Harrison said that each has its advantages. “In general, having the sensor actually worn on your body is always going to provide the best signal. It’s like having the microphone closer to your mouth versus 10 meters away,” says Harrison.
But, there’s a convenience to a tech that works in the background. “There’s something to be said about something that sits on your bedside table that you plug into the wall and it’s there every night, and you don’t really have to think about it,” he adds. “The technologies that are invisible, that even the laziest of us can adopt into our routines, I think are ultimately the most successful.”
1 note · View note