Recently in Coding Tips

ProStores Tips & Tricks »  Coding Tips

Recently, Google updated the javascript code used to track your pages for analysis. The former version was either the secure version or the non-secure version. Now, it's both - which is nice.

There are two bits to the code and here they are:

<script type="text/javascript">
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-XXXXXX-X");
  pageTracker._initData();
  pageTracker._trackPageview();
</script>

You must add your Google Analytics ID to the second block where indicated by the "UA-XXXXXX-X"

If you have an Advanced or better ProStores store, most likely at one time or another you will want to add a logoff link to your navigation. This simple SSML snippet will create just that - it makes a link to log on or log off, depending on the user status.

<ss:if test="$customer.isLoggedIn()">
	<ss:link source="$templateSet.logoff">Log Off</ss:link>
<ss:else/>
	<ss:link source="$templateSet.logon">Log On</ss:link>
</ss:if>

Problem: site stalls at some point while waiting on a script to load.

Reason: The script is referencing a different domain than your own, and if that domain is taking a while to load, your page cannot proceed until it's loaded. This is a problem.

Solution: If the location of the code itself is not important (i.e. code doesn't add any visible elements to the page), place it as far down on the page as possible. That simple change will allow most of the page to load immediately for use - even though the offending slow script isn't done yet.

Alternatively: If the code generates some sort of html and needs to be put in a specific place (i.e. live chat module or site seal) the following tip is very handy indeed. It relies on the "domready" function of MooTools (a supertastic Javascript library) that will only start the slow script after the whole page has loaded. We are using this on our contact us page for the live chat and skype scripts.

1. Download and install MooTools (it's awesome)

2. Replace your module code with something like:

<div id="whereTheCodeEndsUp"></div>

3. Place the "module" code near the bottom of the page inside of something like:

<div id="whereTheCodeStarts" style="display: none;">
	<!-- Module Code here -->
</div>

4. Place the following code directly following the call for mootools.js:

<script language="JavaScript" type="text/javascript">
	window.addEvent('domready', function() {
	$('whereTheCodeEndsUp').innerHTML = $('whereTheCodeStarts').innerHTML;
	$('whereTheCodeStarts').innerHTML = "";
});
</script>

There are many different ways to accomplish this type of thing, but this is pretty quick — especially if you already have MooTools installed.

ProStores has a built in mail processor that it uses to send messages - like the Email a Friend feature for example. The cool part is, you can use the form mail processing that is built in to send your own forms - perhaps a "Contact Us" form or an "Ask a Question" form.

You can use the code below on any ProStores template to create a form that sends a message.


<form name="emailfriend" method="POST" action="Send.bok">

	<p>FROM</p>
	<p>Name <input type="text" name="$$email.senderName" value=" "></p>
	<p>E-Mail <input type="text" name="$$email.senderEmail" value=" "></p>

	<p>TO</p>
	<p>Name <input type="text" name="$$email.recipientName" value=" "></p>
	<p>E-Mail <input type="text" name="$$email.recipientEmail" value=" "></p>

	<hr size="1"/>
	<p>MESSAGE<br><textarea name="$$email.message" cols="40" rows="3" wrap="virtual"></textarea></p>
	<p><input type="submit" value="Send"></p>

</form>

The ".bok" on the action may not be needed - it depends on the type of ProStores installation in which your store lives.

The documentation provided for the ProStores XTE (XML Transaction Engine) authentication scheme is pretty thorough, so I will assume that you are familiar with the variables, and how to receive them into your application. That being said, here is a way to create an XTE 3.0 token using PHP.



$entry_url = {$_GET variable};
$app_id = {$_GET variable or hardcoded};
$secret = {$_GET variable or hardcoded};
$timestamp = time();

$entry_url_pre_sig = "$secret&appid=$app_id&ts=$timestamp";
$entry_url_sig = md5($entry_url_pre_sig);

$complete_entry_url = "$entry_url/getInfo?appid=$app_id" . "&ts=$timestamp" . "&sig=$entry_url_sig";

$result = http_post_send($complete_entry_url, 80, '', 'text/xml', '');        
$responseXML = str_replace('&#174;', "", $result['body']); 
$tree = GetXMLTree($responseXML);

$xml_api = $tree["XTE"][0]["RESPONSE"][0]["XMLAPI"][0]["VALUE"];
$rest_api_non_secure = $tree["XTE"][0]["RESPONSE"][0]["RESTAPINONSECURE"][0]["VALUE"];

$api_ticket = $_GET['ticket'];

$get_token_pre_sig = "$secret&appid=$app_id&ts=$timestamp&ticket=$api_ticket";
$get_token_sig = md5($get_token_pre_sig);

$complete_token_url = $rest_api_non_secure . "auth/getToken?appid=$app_id" . "&ts=$timestamp" . "&ticket=$api_ticket" . "&sig=$get_token_sig";

$token_result = http_post_send($complete_token_url, 80, '', 'text/xml', '');        
$responseXML = str_replace('&#174;', "", $token_result['body']); 
$tree = GetXMLTree($responseXML);

$token = $tree["XTE"][0]["RESPONSE"][0]["TOKEN"][0]["VALUE"];

echo "Your sweet token = $token ";

/*  These facilitate the transmission of data to the xte, and parsing the data into a nice array */

function http_post_send($url, $port, $body, $content_type, $cookie) {
	$url = ereg_replace("^http://", "", $url);
	$host = substr($url, 0, strpos($url, "/"));
	$uri = strstr($url, "/");
	$header = http_post_header($uri, $host, strlen($body), $content_type, $cookie);
	$socket = fsockopen($host, $port, $errno, $errstr);
	fputs($socket, $header.$body);
	$ret = array("header" => array(), "body" => "");
	$reading_header = true;
	$status = array("unread_bytes" => 1);
	while ($status["unread_bytes"] != 0){
		$chunk = fgets($socket);
		$status = socket_get_status($socket);
		if($reading_header){
			if ($chunk == "\r\n"){
				$reading_header = false;
			}
			else{
				$ret["header"][] = $chunk;
			}
		} 
		else{
			$ret["body"] .= $chunk;
		}
	}
	
	while ($more = fgets($socket)){
		$ret["body"] .= $more;
	}
	
	fclose($socket);
	return $ret;
}

function http_post_header($uri, $host, $content_length, $content_type, $cookie) {
	$h  = "POST $uri HTTP/1.0\n";
	$h .= "Host: $host\n";
	$h .= "User-Agent: Mozilla/4.0 [en] (Windows NT 5.0; U)\n";
	$h .= "Accept: */*\n";
	$h .= "Accept-Language: en-us\n";
	$h .= "Accept-Encoding: gzip, deflate\n";
	$h .= "Connection: Keep-Alive\n";
	$h .= "Content-Type: $content_type\n";
	$h .= "Content-Length: $content_length\n";
	$h .= "\n";
	return $h;
}

function GetXMLTree($data) { 
	$parser = xml_parser_create('ISO-8859-1');
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
	xml_parse_into_struct($parser, $data, $vals, $index); 
	xml_parser_free($parser); 
	
	$tree = array(); 
	$i = 0; 
	$tree[$vals[$i]['tag']][] = GetChildren($vals, $i); 
	return $tree; 
} 


function GetChildren($vals, &$i){ 
	$children = array();
	if(isset($vals[$i]['value'])){ 
		$children['VALUE'] = $vals[$i]['value']; 
	}
	while(++$i < count($vals)){ 
		switch ($vals[$i]['type']){ 
			case 'cdata': 
				if(isset($children['VALUE'])){
					$children['VALUE'] .= $vals[$i]['value'];
				}
				else{
					$children['VALUE'] = $vals[$i]['value'];
				}
				break;
			case 'complete': 
				if(isset($vals[$i]['attributes'])) {
					$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
					$index = count($children[$vals[$i]['tag']])-1;
				
					if(isset($vals[$i]['value'])){ 
						$children[$vals[$i]['tag']][$index]['VALUE'] = $vals[$i]['value'];
					}
					else{
						$children[$vals[$i]['tag']][$index]['VALUE'] = '';
					} 
				} 
				else{
					if(isset($vals[$i]['value'])){ 
						$children[$vals[$i]['tag']][]['VALUE'] = $vals[$i]['value'];
					} 
					else{
						$children[$vals[$i]['tag']][]['VALUE'] = '';
					} 
				}
				break; 
			case 'open': 
				if(isset($vals[$i]['attributes'])){
					$children[$vals[$i]['tag']][]['ATTRIBUTES'] = $vals[$i]['attributes'];
					$index = count($children[$vals[$i]['tag']])-1;
					$children[$vals[$i]['tag']][$index] = array_merge($children[$vals[$i]['tag']][$index],GetChildren($vals, $i));
				} 
				else{
					$children[$vals[$i]['tag']][] = GetChildren($vals, $i);
				}
				break; 
			case 'close': 
				return $children; 
		} 
	} 
} 

This post may stray from my usual uber-helpful code snippet. But, it could save you unbelievable amounts of time so pay very close attention...

The ampersand character is known around here as the devil symbol. Not the word "ampersand" mind you, but ---> & <---- this evil guy. Stay away from it unless you're using it to escape your HTML. The word 'and' is only two characters longer. In fact, in order to produce that symbol, you have to hold down the shift key, and press 7 which definitely takes more energy and concentration. So the next time your xml_parse_into_struct fails to push data into the array, check to see that your sweet & sour chicken is both sweet and sour.

P.S. all symbols are stupid. Stay away from them.
P.P.S. Don't ever use Word for web text. It's stupid, and has stupid characters.

About this Archive

This page is an archive of recent blog entries in the Coding Tips category.

Design Tips is the next category.

Find recent content on the main index or look in the archives to find all content.