Hack25.Use Query Strings Effectively


Hack 25. Use Query Strings Effectively

The query string (the stuff after the "?" in a URL) is a powerful tool for web measurement and data collection.

Because of the stateless nature of HTTP, many sites use query strings in their URLs to pass information from page to page or request to request. The query string is a specific portion of a URI that begins after the URI stem and a question mark (?), as shown in the following example:

http://www.mysite.com/page.htm?name=value

Where www.mysite.com is your server DNS or (s-dns) name, page.htm is your URI stem and name=value is your query string. The query string in this example contains one query string, which is composed of a parameter, variable, or name=value pair.

Within the structure of a query string, there may be multiple pairs, each consisting of a variable, followed by an equal sign (=) and then the data or value associated with each variable name. If there are multiple query string variables in your query string, they will be separated by an ampersand (&). Simply put, the query string is a sequence of name=value pairs separated by ampersands (&) as in:

 ?page=product%20view%20page%2Fprodid123&campaign=123 

Query strings can be used to carry almost any type of data back to your web servers for a very wide range of purposes. Any query string parameters added to the URL typed into the address bar of the web browser will likely be captured and logged along with the rest of the URL by a responding web server. Finally, certain "unsafe" characters (generally, non-alphabetic or non-numeric characters) in the value of a query string must be encoded to ensure that the data will be correctly interpreted across all platforms. For example, the "space" character must be encoded as its hexadecimal equivalent (%20) or as a + sign.

2.13.1. Some Common Uses for the Query String in Web Measurement

The query string may be used for a wide variety of web measurement purposes, and the figures in this section provide just of few common examples.

2.13.1.1 Properly identifying dynamically generated pages.

In some dynamic web sites one or more name=value pairs in the query string of a URL may be important to determining the actual page requested by a visitor. For instance, many URLs for dynamic pages (those generated by a script on the server, rather than a static file) are structured like those below:

http://www.mysite.com/pageserved.asp?
PAGENAME
=HOME
http://www.mysite.com/pageserved.asp?
PAGENAME
=NEXTPAGE

This structure often results from the use of personalization or dynamic content management systems, in which the actual content served in a page is determined on the fly by the URI, the cookie, related data, and application logic. In the previous example, PAGENAME indicates what page will be served to the requester of this URI.

Many web measurement systems can be configured to use the query names to define unique pages. This is important because most measurement systems would otherwise interpret all requests for pageserved.asp as the same page, despite the fact that the page serves different content (identified by the PAGENAME attribute in the query string).

2.13.1.2 Identifying marketing campaign response.

Site developers and applications often add many query string variables into a site's URLs that both identify the actual page to be served and provide additional information about which marketing campaign [Hack #37] caused a visitor to see and then select the particular URL. An example of this would be:

http://www.myserver.com/pageserved.asp?
PAGENAME
=HOME&
CAMPAIGN
=10001
2.13.1.3 Collecting HTML form data for analysis.

You'll often be interested in analyzing information that visitors enter into forms on your site. Typically, this information is passed to the web server within a POST type request, signified as follows:

 <form method="POST" action="formthankyou.asp"> 

Modifying the method from a POST request to a GET request results in the form values entered being displayed as query string name=value pairs within the web browser address bar on the resulting formthankyou.asp page (Figure 2-5)

The result of modifying the form action value from a POST request to a GET may not be desirable given the fact that the entered values are displayed within the address bar as illustrated in Figure 2-5. Using JavaScript within the form page, a POST method can be used, while still allowing you to collect the provided information as a part of a query string, by appending the posted form values as query string parameters to the action value as illustrated in Figure 2-6.

Figure 2-5. Sample query string


Figure 2-6. Using POST data with a query strings


The following JavaScript code modifies the POST action value:

 <form method="POST" action="formprocesspage.asp" onSubmit="AppendFormValues();"> <script language="JavaScript"> var formvalues=""; function AppendFormValues() { for (i=0; i<document.forms[0].length; i++) { var item = document.forms[0].elements[i]; if (item.type!="hidden" && item.type!="submit" && item.name!="undefined"){ var formitem = item.name; var formvalue = item.value; formvalues += formitem + "=" + formvalue + "&"; } } document.forms[0].action = document.forms[0].action + "?" + formvalues; } </script> 

The example shown in Figure 2-6 results in three requests to the web server: formentrypage.asp, formprocess.asp, and formthankyou.asp. Inserting the JavaScript example in the formentrypage.asp file will modify the form action value to contain query string name=value pairs corresponding to the formfield=enteredvalue submitted by the visitor. In the example, the request for formprocess.asp will be appended with a query string containing the formfield=enteredvalue pairs, but they will not be presented to the visitor within the web browser address bar. The request will be made to the web server for formprocess.asp as follows:

 http://www.mysite.com/formprocess. asp?firstname=Joe&lastname=Smith&state=VA&zip=20148 

2.13.2. Putting the Query String to Work

To take advantage of the three most common examples described above, the critical step is knowing how your particular measurement application allows you to access information contained in the query string. Unfortunately, not all applications make it easy; it is very common for page tag-based applications to require you to transcribe information out of the query string and into custom variables [Hack #31] before that data can be passed to the data collection device. The following code, written using VBScript and JavaScript, demonstrates the most common strategy for this type of translation:

 rev="<%= request.querystring("dollar_value") %>"; <!-- Change 0.00 to the actual value of the transaction --> document.write('<img src="/books/4/263/1/html/2//cgi-bin/readtag.pl?url='+escape(document. location)+'&amp;ref='+escape(document.referrer)+'&amp;rev='+rev+'">'); 

The request.querystring("dollar_value") in the code demonstrates an easy solution to get the document's query string values inserted one by one into the necessary JavaScript code.

Logfile analyzers and hybrid solutions usually make this process easier, providing you some type of interface into the different name/value pairs found in the logfile and allowing you to generate reports based on that data (Figure 2-7).

In this case, you would simply identify a particular "name" in the name/ value pair and tell your log analyzer how to deal with that information. This is the essence of tracking URLs [Hack #43], which are so critical to most marketing measurement and analysis.

If your web pages are dynamically generated, a common practice for nearly all retail web sites, you need to ensure that relevant information contained in your URLs is translated and made available for analysis. Because there is so much variation in the measurement marketplace in how query strings are made available for analysis, we strongly recommend you contact your vendor for specifics.

Jim MacIntyre and Eric T. Peterson

Figure 2-7. "Search terms with Revenue" report, based on query-string parameters




    Web Site Measurement Hacks
    Web Site Measurement Hacks: Tips & Tools to Help Optimize Your Online Business
    ISBN: 0596009887
    EAN: 2147483647
    Year: 2005
    Pages: 157

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net