Shopping cart and history for partial HTML integration

Background

This refers to code used to manage sessions in versions prior to 15.24. See: guide for upgrading to the current sessions and history plugin.

In order to enable Funnelback’s shopping cart and search history for embedded HTML, it’s necessary to make modifications for handling session cookies. The following provides an illustrative example for how to enable the search history and shopping cart features with embedded HTML. This example is written in PHP and some details are specific to PHP. However, the concepts should apply to any implementation.

Session history

These instructions are for implementations that are already using a PHP wrapper page.

See also:

For PHP specific implementations, any HTML rendering must come after the code in this section.

With embedded HTML, the CMS relays requests between the user and Funnelback servers as a proxy. When a user submits a query, the CMS makes the request to Funnelback servers, receiving the cookie in the response header.

If a user already has a Funnelback cookie in their browser, it’s necessary to send that cookie with the CMS request. In PHP, the cookie value can be identified using the global variable $_COOKIE with the ID user-id.

$request_cookie = $_COOKIE['user-id'];

In the request from the CMS, we need to make sure the cookie is sent. In PHP we use CURL to send the Funnelback cookie:

$curl = curl_init();
	curl_setopt_array($curl, array(
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_URL => $url,
		CURLOPT_TIMEOUT => 5,
		CURLOPT_HEADER => true,
		// Send the FB cookie with the request
		CURLOPT_COOKIE => "user-id=" . $request_cookie
));

If this is the first time a user has searched, there’s no cookie and that field will be empty. Then the CMS will receive a response from the Funnelback servers and we need to make sure that we get the header of that response in order to identify the cookie.

$response = preg_split("/\n\n/", curl_exec($curl), 2);

We then split the lines of the header into an array.

$header_lines = preg_split("/\n/", $response[0]);

Now it’s possible to iterate through the array of the response header and identify the cookie. Once the cookie is identified, it’s necessary to set the domain and path so that the browser sends the cookie when searches are performed anywhere on the domain.

foreach ($header_lines as $value) {
	if (strpos($value, 'Set-Cookie') !== false) {
		$cookie = rtrim($value) . ";Domain=EXAMPLE_DOMAIN.COM;Path=/";
		header($cookie);
	}
}

This code can be inserted within the additional code that’s described in the example PHP wrapper.

Shopping cart

The Funnelback shopping cart utilizes AJAX requests, requiring further modifications. First, it’s necessary to use a version of Angular that supports the withCredentials flag on AJAX requests (version 1.1.2 supports this). After that, it’s necessary to make three changes to Funnelback’s javascript code used for AJAX requests, funnelback-session.js.

  • The funnelback-session.js must be modified so that the config removes the X-Requested-With header and the session cookie is passed. This can be inserted after the .module settings.

    .config(['$httpProvider', function($httpProvider) {
    	delete $httpProvider.defaults.headers.common["X-Requested-With"];
    	$httpProvider.defaults.withCredentials = true;
    }])
  • It’s necessary to indicate an absolute path for the cart.

    var cartResource = $resource('//EXAMPLE_DOMAIN.COM/s/cart.json'
  • Adjusting the path and adding the cookie must also be done for AJAX requests to clear the cart.

    var historyResource = $resource('//EXAMPLE_DOMAIN.COM/s/:op-history.json',
    {collection: collection},
    {
        clear: {method: 'DELETE', params: {collection: collection}, withCredentials: true},
    });