Redirector

The EVRYTHNG Redirector allows developers to add some context-aware logic to any short URL that is associated with a Platform Thng or product resource. This enables the ability to change where a short URL will direct the browser to depending on many context items, such as time, location, the Thng or product in question, the user themselves, as well as constant data.


Example Use Case

The example that will be explored here is that of a 'smart' advertising flyer (or leaflet) that contains a QR code encoding the short URL of an EVRYTHNG product. Such leaflets can be distributed at airports, events, or on the street. The flyer itself contains a call-to-action inviting the reader to scan the QR code with their mobile phone in order to receive contextualised information on recommended travel destinations for the specific country they are in.

The idea here is to produce only one version of the flyer, but make it link to specialised content using the programmable nature of the Redirector and so producing a different redirection URL depending on the country of the reader who has scanned the QR code. A simple example of such a flyer is shown below:


Solution Overview

To enable this contextualised experience, the following must be set up inside an EVRYTHNG account:

  • A project to group the resources specific to this walkthrough, and an application representing this solution to contain the Redirector functionality.
  • A product to represent the printed flyer containing the short URL and QR code that will be printed.
  • An application-level Redirector containing the country-specific rules for URL redirection based on location.
  • An account-level Redirector to delegate anonymous (third-party QR code scanners) visitors to the correct application-level Redirector.

Once all these components are set up, a scan of the product's QR code will result in a contextualised experience of recommending travel destinations based on the scanning user's country.


Create a Project

Create a project to group and scope the walkthrough resources:

 Via the Dashboard

  • Log into the EVRYTHNG Dashboard (or create an account if you do not already have one).
  • Choose the 'Create project' icon in the left-hand navigation pane, next to 'Projects'.
  • Give the project a suitable name, such as 'Redirector Walkthrough', and choose 'Create Project' to finish.

Via the API

curl -i -H "Content-type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X POST 'https://api.evrythng.com/projects' \
  -d '{ 
    "name": "Redirector Walkthrough" 
  }'

Create an Application

Create an application that will contain the Redirector rules allowing a dynamic redirection experience:

Via the Dashboard

  • Ensure the project previously created is still selected in the dropdown at the top of the navigation pane.
  • Choose 'Applications', then 'Add new' at the top of the page to create a new application resource.
  • Give the application a suitable name, such as 'Redirector Walkthrough Application', and choose 'Create application' to finish. On the resulting page, the section titled 'Redirector' is where the contextual rules will be placed later on in this walkthrough.

Via the API

Use the id from the project just created to specify which should contain the new application.

Substitutions: :projectId

curl -i -H "Content-type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X POST 'https://api.evrythng.com/projects/:projectId/applications' \
  -d '{ 
    "name": "Redirector Walkthrough Application",
    "socialNetworks": {}
  }'

Create a Product

The next step is to create a new product resource to represent the type of flyers that will be printed. Since they are all identical, a single resource with a single QR code will be an efficient way to link the physical item to the digital counterpart in the EVRYTHNG Platform.

Via the Dashboard

  • Ensure the project previously created is still selected in the dropdown at the top of the navigation pane.
  • Choose 'Products', then 'Add new' at the top of the page to create a new product resource.
  • Give the product a suitable name, such as 'Explore Europe Flyer', description, and brand name, and choose 'Create product' to finish.
  • Optionally, set the product image to differentiate it from other products in the account.

Via the API

Use the id from the project just created to scope the resource to the correct project upon creation.

Substitutions: :projectId

curl -i -H "Content-Type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X POST 'https://api.evrythng.com/products?project=:projectId' \
  -d '{
    "name": "Explore Europe Flyer",
    "description": "A type of flyer recommending to scan for information on local attractions in European countries.",
    "brand": "HighFlyers Inc.",
    "photos": [
      "https://www.printexpress.co.uk/images/home/product-02-leaflet.jpg"
    ]
  }'

Create a Redirection

Once the product is created, a redirection must be created that is associated with it. The result of this operation is a short URL that can be navigated to by any browser, and a QR code to facilitate this navigation. A reader of the flyer who scans the QR code will be redirected to the short URL by the third-party scanner app, and in turn to an appropriate travel destinations page by the Redirector, after it is set up.

Via the Dashboard

  • Navigate to the product's details page within the 'Products' section in the navigation pane.
  • Choose 'Setup redirection' and enter a placeholder URL in the 'redirect expression field'. This will be the result of redirection in the event that no other rules match.
  • Choose 'Create redirection' to finish. Once this is done, a QR code and short URL will be generated and shown on the product's details page. Try these now to check that they redirect as expected.

Via the API

Use the id of the product just created to specify which product will receive the redirection.

Substitutions: productId

curl -H "Content-Type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X POST 'https://api.evrythng.com/products/:productId/redirector' \
  -d '{
    "defaultRedirectUrl": "https://example.com?shortId={shortId}"
  }'

Optionally, verify the redirection is set up by navigating to the product's details page in the EVRYTHNG Dashboard, or by navigating to the short URL, whose components are returned in the response to the API request above. For example: https://tn.gg/ZQmS3S2PFA.


 Set up the Application-level Redirector

There are two main types of Redirector in an EVRYTHNG account:

  • Account-level Redirector - accepts anonymous requests to short URLs and either produces a redirection, or delegates to some application within the account.
  • Application-level Redirector - accepts project/application scoped requests (such as by an Application User API Key) and produces a redirection.

Set up the application-level Redirector to contain the actual rules that will govern the scanning user's final destination URL. There are five example cases provided, but this can easily be scaled to accommodate many more countries.

 Via the Dashboard

  • Navigate to the application's details page, and scroll down to the 'Redirector' section.
  • For each of the rules in the table above:
    • Choose 'Create rule' from the application details page.
    • Choose a suitable name (such as the name of the country).
    • Add a condition that the country is the rule's country code (such as 'GB').
    • Enter the destination URL in the 'Redirect to' field.
    • Choose 'Create rule' to finish that rule.
  • Lastly, add a final 'catch all' rule for those users not in any of the above countries, redirecting to a general European travel page, and then use the arrow buttons to move it to the end of the list, to be processed last.

Via the API

It is possible to install all the rules in one request. Note the additional 'Other' rule that will be matched in the event the scanning user is not in any of the above countries, redirecting to a general European travel page.

Substitutions: projectId, applicationId

curl -i -H "Content-Type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X PUT 'https://api.evrythng.com/projects/:projectId/applications/:applicationId/redirector' \
  -d '{
    "rules": [{
      "match": "country=IT",
      "name": "Italy",
      "redirectUrl": "http://www.italia.it/en/discover-italy.html"
    }, {
      "match": "country=DE",
      "name": "Germany",
      "redirectUrl": "http://www.germany.travel/en/towns-cities-culture/towns-cities/towns-cities.html"
    }, {
      "match": "country=ES",
      "name": "Spain",
      "redirectUrl": "http://www.spain.info/en_GB/que-quieres/ciudades-pueblos/"
    }, {
      "match": "country=FR",
      "name": "France",
      "redirectUrl": "http://uk.france.fr/en/dispatch/destinations"
    }, {
      "match": "country=GB",
      "name": "Great Britain",
      "redirectUrl": "https://www.visitbritain.com/gb/en/destinations"
    }, {
      "name": "Other",
      "redirectUrl": "https://www.lonelyplanet.com/europe"
    }]
  }'

 Setup the Account-level Redirector

With the application-level Redirector set up, and project/application scoped requests to the will be short URL will be redirected as intended. However, requests coming from third party scanning apps will be anonymous, and will not be handled by the application-level Redirector directly.

To bridge the gap, the account-level Redirector can delegate a redirection decision to any application based on a large variety of contextual data. In this example, the product ID will be used to delegate requests to the 'Explore Europe Flyer' product QR code or short URL to the application-level Redirector, which will product the correct destination URL based on the user's location.

 Via the Dashboard

  • Navigate to the product's details page, and copy its ID.
  • Choose 'Redirector' from the left-hand navigation pane.
  • Choose 'Create rule' and add a condition that the product ID is the one that was just copied and represents the type of flyer being printed.
  • Select the 'Let application decide' drop-down field, and select the application that contains the country-based Redirector rules.
  • Choose 'Create rule' to finish setting up the account-level Redirector.

Via the API

🚧

Note

If there are any existing account-level Redirector rules present in the account, ensure the rules array shown below is combined with those rules that are already present, else they will be overwritten.

Substitutions: projectId, applicationId, productId

curl -i -H "Content-Type: application/json" \
  -H "Authorization: $OPERATOR_API_KEY" \
  -X PUT 'https://api.evrythng.com/redirector' \
  -d '{
    "rules": [{
      "match": "product.id=:productId",
      "name": "Redirector Walkthrough Delegation",
      "delegates": [{
        "app": ":applicationId",
        "project": ":projectId"
      }]
    }]
  }'

Conclusion

Now that all the moving parts of the solution are set up, it's time to test it out. Navigate to the product's details page in the EVRYTHNG Dashboard, imagine you'd been handed the flyer at an airport on your first day on holiday, and scan the QR code.

Depending on which country the originating request comes from, a different final destination page will be shown. Depending on the requirements of the solution, more Redirector rules can be added for more European countries, or even in a more general scope, such as the rest of the world!

See the pages below for more information on the Platform features involved in this walkthrough:

Updated 2 years ago

Redirector


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.