Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "urchin"

Google Analytics / Urchin helper functions

These functions should streamline the process of integrating Google Analytics into your site.

The Code

<?php
 
/**
 * A collection of helper functions for attaching analytics tracking code to
 * links. Used for outbound links and downloads. Functions all produce normal
 * links when analytics is turned off in the configuration.
 * 
 * These functions rely on two configuration variables and the layout code,
 * below. The following variables are required in your app.yml file:
 * 
 * <code>
 *   prod:
 *     analytics:
 *       enabled: on
 *       uacct: UA-XXXXXXX-X
 *   stage:
 *     analytics:
 *       enabled: on
 *       uacct: UA-XXXXXXX-Y
 *   all:
 *     analytics:
 *       enabled: off
 * </code>
 * 
 * Add this code to your layout files for any pages you want to track using
 * Urchin. This could also be done using a filter. Notice you can include a
 * parameter in your response object, 'ut_param', that will be passed to the
 * initial urchinTracker call, if it's present.
 * 
 * <code>
 *     <?php if(sfConfig::get('app_analytics_enabled')): ?>
 *     <script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script>
 *     <script type="text/javascript">
 *     _uacct = "<?php echo sfConfig::get('app_analytics_uacct') ?>";
 *     urchinTracker(<?php if($ut_param = sfContext::getInstance()->getResponse()->getParameter('ut_param')) echo "\"$ut_param\"" ?>);
 *     </script>
 *     <?php endif; ?>
 * </code>
 * 
 * @package     Urchin Helper Functions
 * @subpackage  helper
 * @author      Kris Wallsmith <kris [dot] wallsmith [at] gmail [dot] com>
 * @version     SVN: $Id$
 * @copyright   Have at it ...
 */
 
use_helper('Url', 'Javascript');
 
/**
 * Build a link that includes a call to the Javascript urchinTracker function.
 * 
 * Usually used when linking off your site or to any file on your site that
 * does not include tracking code (i.e. PDF documents, images, etc.)
 * 
 * @see     sfConfig::get('app_analytics_enabled')
 * 
 * @param   (string) $name - name of the link
 * @param   (string) $internal_uri - module/action or @rule
 * @param   (string) $urchin_uri - custom path for urchinTracker
 * @param   (array)  $options - additional HTML parameters
 * 
 * @return  String
 */
function urchin_link_to($name, $internal_uri, $urchin_uri = null, $options = array())
{
    // Only add onClick handler if analytics is enabled.
    if(sfConfig::get('app_analytics_enabled')) 
    {
        // Determine urchin uri.
        if(!$urchin_uri)
        {
            $urchin_uri = url_for($internal_uri);
        }
 
        // Add onClick.
        $new_onclick = 'urchinTracker(\'' . $urchin_uri . '\');';
 
        $options = _parse_attributes($options);
        $options['onclick'] = isset($options['onclick']) ? 
            ($new_onclick . $options['onclick']) :
            $new_onclick;
    }
 
    return link_to($name, $internal_uri, $options);
}
 
/**
 * Build a link to a Javascript call, including a call to urchinTracker.
 * 
 * @see     sfConfig::get('app_analytics_enabled')
 * 
 * @param   (string) $name - name of the link
 * @param   (string) $function - Javascript code
 * @param   (string) $urchin_uri - custom path for urchinTracker
 * @param   (array)  $options - additional HTML parameters
 * 
 * @return  String
 */
function urchin_link_to_function($name, $function, $urchin_uri, $options = array())
{
    // Create link_to_function.
    $link = link_to_function($name, $function, $options);
 
    // Only add onClick handler if analytics is enabled.
    if(sfConfig::get('app_analytics_enabled')) 
    {
        $link = str_replace('onclick="', 'onclick="urchinTracker(\'' . $urchin_uri . '\');', $link);
    }
 
    return $link;
}
 
?>
by Kris Wallsmith on 2007-05-16, tagged analytics  helper  urchin