Thursday, July 19, 2012

Get Previous Value

Occasionally, I come across the need to capture the previous value of something:
  • Page names
  • Internal search term
  • Step in a conversion funnel
  • Etc...
Often, this is something that needs to persist across pages, sessions or perhaps even longer.  To make this happen - and to remain vendor agnostic - I've created the following plug-in that can be added to your custom analytics JavaScript library:
Get Previous Value

The Plug-In


The Get Previous Value plug-in requires the following parameters/input:
$tG.getPreviousValue('[variable to capture]','[cookie name]','[cookie expire days]');
  1. Variable to capture - This is simply the variable you wish to track.  For example, if you are looking for the previous page name, you would pass in your page name variable.
  2. Cookie name - This is the name that will be used to store the variable value passed in in step 1 above.  By default, the cookie persists for the duration of the session.
  3. Cookie expire days - If the cookie should persist for longer than the current session, use this parameter to specify the number of days.  This is an optional parameter.
Assuming all necessary parameters are provided, the Get Previous Value plug-in performs the following steps when called:
  1. Retrieves whatever data was last stored in the cookie.
  2. Updates cookie with new value (passed in via "variable to capture" parameter mentioned above).  If no value, an empty string ("") is stored in the cookie.
  3. Returns the value identified in step 1.  (If "" was captured in step 2, "" will be returned here.)

Example


Get previous page name on first page of site visit:
var pageName="page1";
var prevPageName=$tG.getPreviousValue(pageName,'pv_pn');
Result:  prevPageName==""
Get previous page name on second page of site visit:
var pageName="page2";
var prevPageName=$tG.getPreviousValue(pageName,'pv_pn');
Result:  prevPageName=="page1"
NOTE:  The $tG.getPreviousValue() method has dependencies on the following plug-ins:

No comments:

Post a Comment