Transmitting basket data via server side Webhooks

Transmitting basket data via server side Webhooks

Before you start

If you want to transfer Basket data via server side Webhooks, you have two options:

  1. Use the #{BASKET} parameter to have the whole basket transferred as a single URL-encoded JSON string. This is easier to implement, but usually requires more effort on the receiving end.

  2. If you cannot use option 1, then you have the option to access parts of the baskets and send them as ordered lists.

Sending JSON via the #{BASKET} parameter

Let’s say you have the following target URL configured in your Target Configuration:

1 yourcode.tracking.org?basket=#{BASKET}

After the system computed a conversion, it will call the generated target URL:

1 yourcode.tracking.org&basket=%5B%7B%22pid%22%3A%226553%22%2C%22prn%22%3A%22Shoe%22%2C%22brn%22%3A%22Jordan%22%2C%22pri%22%3A%2250.00%22%2C%22qty%22%3A%221%22%2C%22trc%22%3A%22default%22%2C%22prc%22%3A%22shoes%22%7D%2C%7B%22pid%22%3A%2212355%22%2C%22prn%22%3A%22Shirt%22%2C%22brn%22%3A%22Addidas%22%2C%22pri%22%3A%2270.00%22%2C%22qty%22%3A%222%22%2C%22trc%22%3A%22default%22%2C%22prc%22%3A%22shirts%22%7D%5D

Beautiful, isn’t it? The given basket is, as mentioned, a URL-encoded string. When encoded (and formatted for readability) it will look like this:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [{ "pid": "6553", "prn": "Shoe", "brn": "Jordan", "pri": "50.00", "qty": "1", "trc": "default", "prc": "shoes" }, { "pid": "12355", "prn": "Shirt", "brn": "Addidas", "pri": "70.00", "qty": "2", "trc": "default", "prc": "shirts" }]

That’s it!

Sending the basket as lists via the #{BASKET_ARRAY} parameters

If, for some reason, you cannot compute JSON on the receiving end, you have the option of sending the basket elements as lists.

The key elements to this method are the two parameters #{BASKET_ARRAY_BEGIN} and #{BASKET_ARRAY_END}. These work in conjunction, and the actual basket parameters you want to receive have to be placed between the two.

The following Parameters are used to declare basket values:

  • BASKET_VALUE_pid → product Id of the basket item

  • BASKET_VALUE_qty → quantity of the basket item

  • BASKET_VALUE_pri → price of the basket item

  • BASKET_VALUE_pri_cents → price of the basket item in cents (without decimals)

  • BASKET_VALUE_prn → product name of the basket item

Let’s look at a simple example:

Let’s say you have the following Target URL configured in your Target Configuration:

1 yourcode.tracking.org?basket=#{BASKET_ARRAY_BEGIN}#{BASKET_VALUE_pid}#{BASKET_ARRAY_END}

Given a conversion with the following basket:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 [{ "pid": "6553", "prn": "Shoe", "brn": "Jordan", "pri": "50.00", "qty": "1", "trc": "default", "prc": "shoes" }, { "pid": "12355", "prn": "Shirt", "brn": "Addidas", "pri": "70.00", "qty": "2", "trc": "default", "prc": "shirts" }]

The system would generate the following URL:

1 yourcode.tracking.org?basket=6553%2C12355

Again, URL decoded it would look like this:

1 yourcode.tracking.org?basket=6553,12355

You can also define a separator (currently available options are ,;|) on your #{BASKET_ARRAY_BEGIN} declaration:

1 yourcode.tracking.org?basket=#{BASKET_ARRAY_BEGIN separator='|'}#{BASKET_VALUE_pid}#{BASKET_ARRAY_END}

Would generate (skipping the URL-decode part)

1 yourcode.tracking.org?basket=6553|12355

 

Now let us look at two more complex examples that show you ways to transfer baskets to your system.

1 yourcode.tracking.org?pid=#{BASKET_ARRAY_BEGIN separator='|'}#{BASKET_VALUE_pid}#{BASKET_ARRAY_END}&qty=#{BASKET_ARRAY_BEGIN separator=';'}#{BASKET_VALUE_pri}#{BASKET_ARRAY_END}&pri_cents=#{BASKET_ARRAY_BEGIN separator=';'}#{BASKET_VALUE_pri_cents}#{BASKET_ARRAY_END}&prn=#{BASKET_ARRAY_BEGIN}#{BASKET_VALUE_prn}#{BASKET_ARRAY_END}

Would generate:

1 yourcode.tracking.org?pid=6553|12355&qty=1,2&pri_cents=5000;3500&prn=Shoe|Shirt

 

As soon as you have created this code, all you need to do is implement it following the instructions in the article on the Webhooks feature.