Collapse Menu
Docs Home
Extensibility Options
Contact Support

Populating Transaction Data via URL Parameters

Overview

This article explains how you can use JavaScript to read the URL of your page and apply data from URL parameters to the Store Builder Library session.

If you use the Store Builder Library (SBL) on your website, you can optionally populate certain pieces of transaction data via URL parameters. 

Parameters are values passed as part of the URL that customers use to access the store page. By adding JavaScript code to parse the URL and apply the parameter values to SBL functions, you pass transaction data right in the URL. This approach is possible for any transaction data that can be applied via SBL functions, including:

  • Products
  • Customer First Name
  • Customer Last Name
  • Coupon Code

Here is an example that you could implement near the bottom of your page, just after the closing </body> tag.

 Note

The following example is provided "as is" and is intended for illustration purposes. You may need to adjust the code provided in the example to meet your needs or work with your specific implementation.
Example of populating transaction data via URL parameters
<script>
    var QueryString = function () {
  // This function is anonymous, is executed immediately and 
  // the return value is assigned to QueryString!
  var query_string = {};
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
        // If first entry with this name
    if (typeof query_string[pair[0]] === "undefined") {
      query_string[pair[0]] = decodeURIComponent(pair[1]);
        // If second entry with this name
    } else if (typeof query_string[pair[0]] === "string") {
      var arr = [ query_string[pair[0]],decodeURIComponent(pair[1]) ];
      query_string[pair[0]] = arr;
        // If third or later entry with this name
    } else {
      query_string[pair[0]].push(decodeURIComponent(pair[1]));
    }
  } 
  return query_string;
}();
</script>
    <script>
        $( document ).ready(function() {

			var s = {};
			
            if(QueryString.product) {
                s = {
                    'products' : [
                        {
                            'path':QueryString.product,
                            'quantity':1
                        }
                    ]
                }
            }
 
            if(QueryString.email && QueryString.fname && QueryString.lname) {
				s.paymentContact = {};
				s.paymentContact.email = QueryString.email;
				s.paymentContact.firstName = QueryString.fname;
				s.paymentContact.lastName = QueryString.lname;
			}
 
            if(QueryString.coupon) {
				s.coupon = QueryString.coupon;
            }
            
			if (s) fastspring.builder.push(s);
        });
    </script>

The above example allows you to use URL parameters to pass information to be added to the cart session. If you specify the product in the URL, be sure to use the product path/product ID rather than the product's Display name.

For example: 

Example URL
.../cart.html?product=8675309&coupon=FALLSALE&fname=Leeroy&lname=Jenkins&email=ne1@all.com

In this example, the store page on your website would have a URL ending in cart.html. The name of the page can be whatever you need. The above adds product ID 8675309 to the cart, applies coupon FALLSALE, and applies the customer's first name, last name, and email address to the order.

If you only want to pass the product:

Example URL
.../cart.html?product=8675309

If you only want to pass the product and the coupon code:

Example URL
.../cart.html?product=8675309&coupon=FALLSALE

If you pass in the customer name or email address, then currently, the first name, last name, and email address must all be passed for any of them to work. For example:

Example URL
.../cart.html?fname=Leeroy&lname=Jenkins&email=ne1@all.com

Try FastSpring

Get a free account and see why FastSpring is the ecommerce partner of choice for software providers around the world. Try our full-service ecommerce solution today to unlock revenue growth for your online company.