URLs in anvil, redirection

What I’m trying to do:
I’m trying to generate urls for affiliates. An affiliate is a role. When an affiliate logs in for the first time, I need to generate an url that would be unique to him, but would also redirect to the homepage of the app.

The affiliate would promote our product and get users to sign up.

Then when a new user that comes from that URL signs up, we need to ‘count’ it on the affiliate role (+1)

What I’ve tried and what’s not working:
I’m somewhat familiar with how Django deals with urls. But Anvil is a one-page app. So none of that would apply.

I’d like to use the default User service from Anvil. The entry point form is something we can set in the IDE to the ‘login’ form. What I don’t understand is how to have multiple login forms and assign them multiple urls. This might go ‘against the grain’ as urls is not something that defines navigation in a one-page app.

This might be a workaround: Could I programatically generate forms that are clones of each other, but with a prepopulated field that is the affiliate UUID? I still need to have some way to give each affiliate a handle to get to that form. Which I don’t see right now.

Sorry if this is too basic a question…

Good thoughts. You’re going to want to take a look at @stucork’s HashRouting dependency. It’s a work of art, and it can support this use case.

With HashRouting, your approach will definitely work. You can create a handler that catches affiliate referrals, logs the referrer/increments a counter in a Data Table, and then redirects the user to sign up.

2 Likes

I use Anvil’s support for query string parameters to deal with unique URLs.

These docs explain how that works:
https://anvil.works/docs/client/navigation#using-the-url-hash

2 Likes

The custom user auth instructions provide another example of this in action, with some pieces that may be pretty directly applicable: Anvil Docs | Custom user authentication

2 Likes

The problem I just realized: Anvil doesn’t control the root (home) of the domain, only app.domain.com. So when traffic lands on home, they are landing on a wordpress install. then only when they press an ‘apply’ button, Anvil takes over. I’m not sure how to keep the information (a query parameter, let’s say on that referral url) when people are clicking on parts of the wordpress website. They may never click ‘apply’ and that’s fine too.

One solution would be to give the landing page to anvil, and kinda embed the user-facing ‘brochure’ site (wordpress) inside an iframe. That sounds terrible and fragile, but I’m listing all the options as they come to my mind.

It might be this is not a solvable problem. When an affiliate sends traffic to a homepage (not anvil) it’s impossible to connect that visit to the ‘apply’ event that Anvil could track.

I don’t think you can do it using only HTML, but you can retain the query string on the transfer to your app domain (or any domain) with a javascript function. Here’s a simple example:

<html>

<script>
function forwardToAppWithQueryStringIntact(){
    location.href = 'https://your.anvil.app.example/#?' + location.search.substring(1)
}
</script>

<button onclick="forwardToAppWithQueryStringIntact()">Click me</button>

</html>
2 Likes

If your company domain is thecompany.com, you can have the Anvil app set to a subdomain app.thecompany.com. This is not a necessary step, and it’s not available on the free account, but it looks a little more authentic/professional.

Then your affiliate would give an url similar to app.thecompany.com#?id=<UUID>

When the new user clicks on that url, the app starts, gets the UUID from the query parameters, creates a cookie, then what happens later doesn’t matter. It could redirect to thecompany.com or proceed with the sign up. If the new user leaves the app and navigates the wordpress site and later clicks on the generic Apply link which opens the app without hash, the app will start and have the cookie previously set. The next server call will be able to use the cookie to identify the user.

You could even have two apps, one very light that sets the cookie and redirects to wordpress very quickly, without even showing the UI. Then the Apply link on wordpress would launch another app configured to share the cookies with the previous app and proceed with the sign up.

I haven’t tried this, but it should work.

2 Likes

@stefano.menci’s idea of a second app could work, too. You’d have something like affiliate.thecompany.com/#?affid=my-favorite-affiliate, and it should be reasonably manageable since cookies and data tables can be shared by multiple apps. There may be some gotchas if cookies are blocked by the browser or an extension, but that complexity exists for all digital marketing channels.