Zend FR

Consultez la FAQ sur le ZF avant de poster une question

Vous n'êtes pas identifié.

#1 05-03-2009 16:35:56

hthetiot
Nouveau membre
Date d'inscription: 04-05-2007
Messages: 8

GoogleAnalytics View Helper

Un shtit helper pour google Analytics...

Support toutes les options que j'ai pu trouver:

Standard Options
trackPageview, setVar
ECommerce Options
addItem, addTrans, trackTrans
Tracking Options
setClientInfo, setAllowHash, setDetectFlash, setDetectTitle, setSessionTimeOut, setCookieTimeOut, setDomainName, setAllowLinker, setAllowAnchor
Campaign Options
setCampNameKey, setCampMediumKey, setCampSourceKey, setCampTermKey, setCampContentKey, setCampIdKey, setCampNoKey
Other
addOrganic, addIgnoredOrganic, addIgnoredRef, setSampleRate

Helper Usage:

Code:

<?php

    // in layout or init of controller
    $trackerId = '123';
    $googleAnalytics = $this->GoogleAnalytics($trackerId, array(
        array('setVar', 'sex:f'),
        array('setVar', 'member_id:' . $memberId),
    ));

    // from controller or view
    $this->GoogleAnalytics()->setVar($trackerId, 'another_var');
    $this->view->GoogleAnalytics()->setVar($trackerId, 'online:1');

?>
<?php
    // displaying tracker
    echo $this->GoogleAnalytics();
?>

Helper source:

Code:

<?php
/**
 * GoogleAnalytics.php
 *
 * See http://www.scribd.com/doc/2261328/InstallingGATrackingCode
 *
 * @category   BaseZF
 * @package    BaseZF_Framwork
 * @copyright  Copyright (c) 2008 BaseZF
 * @author     Harold Thétiot (hthetiot)
 */

class BaseZF_Framework_View_Helper_GoogleAnalytics
{
    /**
     * Tracker options instance
     */
    static protected $_trackerOptionsByIds = array();

    /**
     * Available Trackers options
     */
    static protected $_availableOptions = array
    (
        // Standard Options
        'trackPageview',
        'setVar',

        // ECommerce Options
        'addItem',
        'addTrans',
        'trackTrans',

        // Tracking Options
        'setClientInfo',
        'setAllowHash',
        'setDetectFlash',
        'setDetectTitle',
        'setSessionTimeOut',
        'setCookieTimeOut',
        'setDomainName',
        'setAllowLinker',
        'setAllowAnchor',

        // Campaign Options
        'setCampNameKey',
        'setCampMediumKey',
        'setCampSourceKey',
        'setCampTermKey',
        'setCampContentKey',
        'setCampIdKey',
        'setCampNoKey',

        // Other
        'addOrganic',
        'addIgnoredOrganic',
        'addIgnoredRef',
        'setSampleRate',
    );

    /**
     *
     * @param string $trackerId the google analytics tracker id
     * @param array
     *
     * @return $this for more fluent interface
     */
    public function GoogleAnalytics($trackerId = null, array $options = array())
    {
        if (!is_null($trackerId)) {
            $this->trackPageview($trackerId);

            if (!empty($options)) {
                $this->addTrackerOptions($trackerId, $options);
            }
        }

        return $this;
    }

    /**
     * Alias to _addTrackerOption
     *
     * @param string $optionsName
     * @param array $optionsArgs
     *
     * @return $this for more fluent interface
     */
    public function __call($optionsName, $optionsArgs)
    {
        if (in_array($optionsName, self::$_availableOptions) === false) {
            throw new BaseZF_Exception('Unknow "' . $optionFunc . '" GoogleAnalytics options');
        }

        if (empty($optionsArgs)) {
            throw new BaseZF_Exception('Missing TrackerId has first Argument on "$this->GoogleAnalytics->' . $optionFunc . '()" function call');
        }

        $trackerId = array_shift($optionsArgs);

        $this->_addTrackerOption($trackerId, $optionsName, $optionsArgs);

        return $this;
    }

    /**
     * Add options from array
     *
     * @param string $trackerId the google analytics tracker id
     * @param array of array option with first value has option name
     *
     * @return $this for more fluent interface
     */
    public function addTrackerOptions($trackerId, array $options)
    {
        foreach ($options as $optionsArgs) {

            $optionsName = array_shift($optionsArgs);

            $this->_addTrackerOption($trackerId, $optionsName, $optionsArgs);
        }

        return $this;
    }

    /**
     * Add a tracker option
     *
     * @param string $trackerId the google analytics tracker id
     * @param string $optionsName option name
     * @param array $optionsArgs option arguments
     *
     * @return $this for more fluent interface
     */
    protected function _addTrackerOption($trackerId, $optionsName, array $optionsArgs = array())
    {
        $trackerOptions = &$this->_getTrackerOptions($trackerId);

        array_unshift($optionsArgs, $optionsName);

        $trackerOptions[] = $optionsArgs;

        return $this;
    }

    /**
     * Get tracker's options by tracker id
     *
     * @param string $trackerId the google analytics tracker id
     *
     * @return array an array of options for requested tracker id
     */
    protected function &_getTrackerOptions($trackerId)
    {
        if (!isset(self::$_trackerOptionsByIds[$trackerId])) {
            self::$_trackerOptionsByIds[$trackerId] = array();
        }

        return self::$_trackerOptionsByIds[$trackerId];
    }

    //
    // Render
    //

    /**
     * Cast to string representation
     *
     * @return string
     */
    public function __toString()
    {
        return $this->toString();
    }

    /**
     * Rendering Google Anaytics Tracker script
     */
    public function toString()
    {
        $xhtml = array();
        $xhtml[] = '<script type="text/javascript">';
        $xhtml[] = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");';
        $xhtml[] = 'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
        $xhtml[] = '</script>';

        $xhtml[] = '<script type="text/javascript">';
        $xhtml[] = 'try {';

        $i = 0;
        foreach (self::$_trackerOptionsByIds as $trackerId => $options) {

            // build tracker name
            $trackerInstance = 'pageTracker' . ($i > 0 ? $i : null);

            // init tracker
            $xhtml[] = 'var ' . $trackerInstance . ' = _gat._getTracker("' . $trackerId . '");';

            // add options
            foreach ($options as $optionsData) {

                // build tracker func call
                $optionName = '_' . array_shift($optionsData);

                // escape options arg
                $optionArgs = array();
                foreach ($optionsData as $arg) {
                    $optionArgs[] = is_numeric($arg) ? $arg : '"' . addslashes($arg) . '"';
                }

                // add options
                $xhtml[] = $trackerInstance . '.' . $optionName . '(' . implode(',', $optionArgs) . ');';
            }

            $i++;
        }

        $xhtml[] = '} catch(err) {}</script>';
        $xhtml[] = '</script>';

        return implode("\n", $xhtml);
    }
}

"Software is like sex: it's better when it's free." L.T

Hors ligne

 

#2 05-03-2009 17:39:51

Matthieu
Nouveau membre
Lieu: Montélimar
Date d'inscription: 13-07-2008
Messages: 9
Site web

Re: GoogleAnalytics View Helper

Merci pour cette contribution. Je vais la tester avec intérêt !

Hors ligne

 

#3 06-03-2009 09:02:53

dmathieu
Membre
Lieu: Lyon, France
Date d'inscription: 09-02-2009
Messages: 50
Site web

Re: GoogleAnalytics View Helper


Il faut aimer les autres, non pour soi, mais pour eux - Proverbe Espagnol

Hors ligne

 

#4 08-03-2009 15:49:19

hthetiot
Nouveau membre
Date d'inscription: 04-05-2007
Messages: 8

Re: GoogleAnalytics View Helper

dmathieu a écrit:

Un autre : http://zfsnippets.com/snippets/view/id/19

Pas de support des différentes options de GA et code pas très logique et aucune possibilité d'être utilisé depuis le controller car pas de __toString...

bof... si tu l'avais codé je verrai un intérêt si c'est pour coller le premier lien google je vois moins l'utilité.

Hors ligne

 

#5 22-06-2009 19:38:30

M4d3L
Nouveau membre
Lieu: Quebec, Canada
Date d'inscription: 22-06-2009
Messages: 8
Site web

Re: GoogleAnalytics View Helper

Tres interessant. j'ai ajouter ce composant comme composant standard de mon projet PhenixApp. Si tu fait des mises a jour, tu peux me le faire savoir directement sur mon forum si tu le veux.

PhenixApp : http://www.phenixapp-project.net

Dernière modification par M4d3L (22-06-2009 19:40:39)


.::M4d3L::.
.::Mon blog sur ZF::.
.::PhenixApp-Project::.

Hors ligne

 

Pied de page des forums

Propulsé par PunBB
© Copyright 2002–2005 Rickard Andersson
Traduction par punbb.fr

Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages