Dynamic urls for redirects / Template urls
Redirect urls often change per user or per page, like https://example.com/farms/8f2c1a/settings where 8f2c1a is different for every user. Instead of a fixed url, you can mark the changing parts with curly braces {}, and Produktly fills them in at the moment the user is redirected.
There are two ways to fill a dynamic part:
- From user attributes:
{user.something}takes the value from the data you send when identifying the user. Works on any page. - From the current page url: any other name, like
{farmId}, copies the matching part of the url the user is on right before the redirect.
From user attributes
For this to work, you have to first identify users. For example:
window.onload = () => {
window.Produktly.identifyUser("some_user_id_9809", {
plan: "startup",
companyId: 42,
farmId: "8f2c1a",
permissions: [{ action: "edit" }],
});
};You can then use any of those values in the url:
| Redirect url in Produktly | Where the user ends up |
|---|---|
https://example.com/{user.id}/dashboard | https://example.com/some_user_id_9809/dashboard |
https://example.com/farms/{user.farmId}/settings | https://example.com/farms/8f2c1a/settings |
https://example.com/company/{user.companyId} | https://example.com/company/42 |
https://example.com/{user.permissions[0].action} | https://example.com/edit |
{user.id} is the user id you pass as the first argument. Everything else after user. should be a value in the metadata object. Nested values use dots ({user.company.name}) and list items use brackets ({user.permissions[0].action}).
If the value is missing for a user, that part of the url is left empty.
From the current page url
Any dynamic part that doesn't start with user. is copied from the url the user is currently on. Produktly looks at where the dynamic part sits in your redirect url and takes the part in the same position of the current url. The name inside the braces is only a label, so {farmId}, {id} and {dynamic} all work the same way.
For example, with the redirect url https://example.com/farms/{farmId}/recommendations/new:
| User is on | Where the user ends up |
|---|---|
https://example.com/farms/8f2c1a | https://example.com/farms/8f2c1a/recommendations/new |
https://example.com/farms/8f2c1a/fields?tab=map | https://example.com/farms/8f2c1a/recommendations/new |
https://example.com/dashboard | https://example.com/farms//recommendations/new |
This only works when the redirect happens on a page whose url has the value in the same position. In the example above, the user has to be on one of the /farms/8f2c1a/... pages. If the redirect can happen anywhere else, for example from your dashboard, send the value as a user attribute and use {user.farmId} instead.
The query string (?tab=map) and anything after # are not included when copying from the current url.
The current url
{currentUrl} inserts the full url of the current page, without the query string. This is handy for passing a return address, e.g. https://example.com/upgrade?returnTo={currentUrl}.