Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "analytics"

Google analytics code by filter

This simple filter automatically adds google analytics code for all pages. It's no longer required to modify all the layouts, just put google code to app.yml and this filter does everything for you.

Create a lib/filters/gaFilter.class.php file:

<?php
class gaFilter extends sfFilter
{
  public function execute($filterChain)
  {
    // Nothing to do before the action
    $filterChain->execute();
    // Find google code and check if current module is not disabled
    if(($gaCode = sfConfig::get("app_ga_code",false)) !== false
    && !in_array($this->getContext()->getModuleName(),sfConfig::get("app_ga_disabled_modules",array()))) {
      //Decorate the response with the tracker code
      $response = $this->getContext()->getResponse();
      $response->setContent(str_ireplace('</body>', $gaCode.'</body>',$response->getContent())); 
    }
   }
}
 

Add the filter to filters.yml :

# insert your own filters here
 
# Google analytics filter
ga_filter:
  class: gaFilter
 

Now you are able to insert GA code into your page. Simply copy the whole code and add it as ga_code option into your app.yml file:

all:
  ...
  ga:
    code: >
      <script type="text/javascript">
      try {
      var pageTracker = _gat._getTracker("...");
      pageTracker._trackPageview();
      } catch(err) {}</script>
 

It works on fact, that you probably will not have a </body> tag in other place except at the end of your page and you will not use it in other content type than text/html.

Optionally you may disable GA filter in specific modules, such as administration is. To do this, add this option to your app.yml:

all:
  ...
  ga:
    disabled_modules: [administration]
    code: >
      ...
 
by Radek Petráň on 2010-03-07, tagged analytics  filter  google 

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