Lebod's avatar

How to do Affiliate Tracking with Laravel

I wanted to get the community's thoughts on this. We have a Laravel based shop and now we need to add affiliate tracking to automate commission tracking etc. We need to basically be able to append a unique URL code for each affiliate to any page on the site and have the affiliate credited if a checkout event happens. I've looked at the major self hosted apps like post affiliate pro, idevaffiliate and some other smaller apps. Would you guys recommend maybe a better app or better way to be able to do this?

0 likes
9 replies
ShaneDRosenthal's avatar

I'm looking for the same thing here. i created a wrapper for our payment system - we require Authorize.net for now but I may be able to get away with stripe in our next plunge and would use cashier for the sales. But... what about commissions/affiliates?

To be honest I am looking for something I can just plug into my site - but really it can't be that hard. Affiliates would get a URL that has a query string that identifies them, and we create some session variable that identifies that individual or company. When a sale goes through we capture the session variable (company/individual identifier) and pass that companies id along with the transaction. Then I would create an admin area for them when they login they would see all of THEIR sales, meaning transaction that have their company id in that row. companies->orders() one-to-many.

Not sure it would be much more difficult than that - maybe show total commission owed, email the store admin that you want a check, you can get creative showing data and notifications to people - tons of that out there.

zoransa's avatar

Hi guys!

I think you two @shanerbaner82 and @Lebod are looking for two opposite things.

@Lebod if you are just looking to 'affiliate links' you can make something like this in your router file make:

Route::get('/goto/{ref}/{customId}', 'as' => 'affiliate_link', function ($ref, $customId) {
    // if you have simple link to one store and only 2 parameters you can do it here 
    return redirect('http://www.example.com/something?affid={$ref}&tag={$customId}");
});

if you need to do DB lookup and other things and go more then one line of code then you could move it to some controller.

when building links on the website you can do

<a href={{ route('affiliate_link', ['rel'=> $product->id, 'customId' => $myAffId]) }}

This is not copy/paste code but you get idea what to do.

The other question regarding simple affiliate tracking well you need custom private affiliate program and it is relatively easy to make it but if you want to add security and features to it it quickly becomes medium to big size project that has to plug into your existing ecommerce so I would rather go after some product that somebody already made and test it then trying to hack something on my own.

bashy's avatar

Threads in Site Improvements won't be looked at by most since it's meant for Laracasts' site

martinbean's avatar

@Lebod All you need to do is look for a query string parameter in URLs and save it to a cookie. Then, when the user checks out, check to see if there’s an affiliate ID in the cookie and if so, apply the appropriate commission.

You can create a service provider or middleware that does the first part:

class AffiliateTrackingMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if ($request->has('affiliate_id')) {
            $response->withCookie(cookie('affiliate_id', $request->get('affiliate_id'), $minutes));
        }

        return $response;
    }

}
10 likes
Lebod's avatar

We tried a lot of different ways of getting this done INCLUDING developing our own in house tracking application and decided that the development cost and time wouldn't be worth it.

We tried a few different applications including one developed on Code Igniter called JROX Jam which we couldn't implement. We finally settled on Post Affiliate Pro 5 with some small tweaks to the order confirmation page and a couple of javascript tracking files for commission tracking. Things are working fine now.

Lebod's avatar

martinbean,

that sounds like an interesting solution... We also need a way to add up the commission totals, send out email notifications to the affiliate, manually add comissions. It ends up being a lot of work...

1 like
secondman's avatar

You can accomplish this pretty easily using some explicit model binding and @martinbean 's middleware above.

I'm currently working on some MLM software for a client using Jetstream/Inertia

First I created a sponsor_id on my user table, this could be referred_by or whatever you like.

Then created a parameter on my index route like so:

Route::get('/{affiliate?}', IndexController::class)->middleware('affiliate');

Then in my route service provider I created an explicit binding:

Route::bind('affiliate', function($value) {
    return User::where('id', $value)
            ->orWhere('username', $value)
            ->firstOrFail();
});

Then in the handle method of the AffiliateTracking middleware I check for the affiliate route:

public function handle(Request $request, Closure $next)
{   
    $response = $next($request);

    if ($sponsor = $request->route('affiliate')) {
        $minutes = 43200;

        $response->withCookie(cookie('sponsor', $sponsor->id, $minutes));
    }

    return $response;
}

Of course using Jetstream and Inertia I don't have direct access to the Fortify registration controller so I simply added the user (sponsor) to Inertia's shared props in the HandleInertiaRequests middleware:

public function share(Request $request)
{
    if ($sponsor = Cookie::get('sponsor')) {
        $sponsor = User::where('id', $sponsor)->firstOrFail();

        $user = [
            'id' => $sponsor->id,
            'name' => $sponsor->name
        ];
    }

    return array_merge(parent::share($request), [
        'sponsor' => $user ?? null,
    ]);
}

No in the registration component I have access to $page.props.sponsor which holds the id and name of the referrer and they can be added to the form props, and displayed as the referrer on the page.

We use Snowflake integers for ids on all our tables and we have usernames, so the binding allows the members to use either their id:

https://example.com/586899456

or their username:

https://example.com/johndoe

The binding resolves either.

With the middleware, you can simply add it to any route so if you have a shopping cart or whatever it will get picked up.

I hope this is useful to someone.

Cheers

Please or to participate in this conversation.