3

Google has released a new product - Google Optimize. Is it possible to modify the standard Universal Analytics code in Magento (enabled under Google API in 1.9.3) to implement the change? The actual change needed is:

Paste into your existing Analytics snippet as shown below:

<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-xxxxxx', 'auto');
ga('require', 'GTM-xxxxxx');
ga('send', 'pageview');
</script>

so only 1 extra line is needed

1

1 Answer 1

0

I've thrown together a small module to add this in via a backend config for CE 1.9.3.x, hopefully it'll be useful to some.

app\etc\modules\Namespace_Googleoptimize.xml

<?xml version="1.0"?>
<!--
/**
 * Class Namespace_Googleoptimize
 *
 * @category    Namespace
 * @package     Namespace_Googleoptimize
 */
-->
<config>
    <modules>
        <Namespace_Googleoptimize>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_Googleoptimize>
    </modules>
</config>

app\code\local\Namespace\Googleoptimize\etc\config.xml

<?xml version="1.0"?>
<!--
/**
* Namespace
*
* @category    Namespace
* @package     Namespace_Googleoptimize
*/
-->
<config>
    <modules>
        <Namespace_Googleoptimize>
            <version>0.1.0</version>
        </Namespace_Googleoptimize>
    </modules>
    <global>
        <helpers>
            <namespace_googleoptimize>
                <class>Namespace_Googleoptimize_Helper</class>
            </namespace_googleoptimize>
        </helpers>
        <blocks>
            <googleanalytics>
                <rewrite>
                    <ga>Namespace_Googleoptimize_Block_Ga</ga>
                </rewrite>
            </googleanalytics>
        </blocks>
    </global>
</config>

app\code\local\Namespace\Googleoptimize\etc\system.xml

<?xml version="1.0"?>
<config>
    <sections>
        <google translate="label" module="googleanalytics">
            <label>Google API</label>
            <tab>sales</tab>
            <frontend_type>text</frontend_type>
            <sort_order>340</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>
            <groups>
                <googleoptimize translate="label">
                    <label>Google Optimize</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>20</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>
                    <fields>
                        <active translate="label">
                            <label>Enable</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                            <sort_order>10</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </active>
                        <container translate="label">
                            <label>Container ID</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>20</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                            <comment>Enter the full container ID, ie GTM-XXXXXXX</comment>
                        </container>
                    </fields>
                </googleoptimize>
            </groups>
        </google>
    </sections>
</config>

app\code\local\Namespace\Googleoptimize\Helper\Data.php

<?php
/**
* Namespace
*
* @category    Namespace
* @package     Namespace_Googleoptimize
 */

class Namespace_Googleoptimize_Helper_Data extends Mage_Core_Helper_Abstract
{

    const XML_PATH_ACTIVE          = 'google/googleoptimize/active';
    const XML_PATH_CONTAINER       = 'google/googleoptimize/container';

     /**
     * Whether Google Optimize is enabled in backend
     *
     * @param mixed $store
     * @return bool
     */

    public function isGoogleOptimizeEnabled($store = null)
    {
        return Mage::getStoreConfigFlag(self::XML_PATH_ACTIVE, $store);
    }

    /**
     * Get Google Optimize Container ID
     *
     * @param string $store
     * @return string
     */
    public function getContainerId($store = null)
    {
        return Mage::getStoreConfig(self::XML_PATH_CONTAINER, $store);
    }


}

app\code\local\Namespace\Googleoptimize\Block\Ga.php

<?php
/**
* Namespace
*
* @category    Namespace
* @package     Namespace_Googleoptimize
 */

class Namespace_Googleoptimize_Block_Ga extends Mage_GoogleAnalytics_Block_Ga
{

    /**
     * Render regular page tracking javascript code
     * The custom "page name" may be set from layout or somewhere else. It must start from slash.
     *
     * @param string $accountId
     * @return string
     */
    protected function _getPageTrackingCodeUniversal($accountId)
    {

        if ($this->helper('namespace_googleoptimize')->isGoogleOptimizeEnabled()) {
            $containerId = $this->helper('namespace_googleoptimize')->getContainerId();
            return "
ga('create', '{$this->jsQuoteEscape($accountId)}', 'auto');
" . $this->_getAnonymizationCode() . "
ga('require', '{$containerId}');
ga('send', 'pageview');
";

        } else {
            return "
ga('create', '{$this->jsQuoteEscape($accountId)}', 'auto');
" . $this->_getAnonymizationCode() . "
ga('send', 'pageview');
";
        }



    }


    /**
     * Is Googel Optimize Enabled
     *
     * @return bool
     */
    protected function _isEnabled()
    {
        return Mage::helper('namespace_googleoptimize')->isGoogleOptimizeEnabled();
    }


}

It's worth noting that this might be the route required for you even if you are using Tag Manager.

Despite the fact that Tag Manager has a default tag for Google Optimize built in, their own docs say;

While it’s possible to deploy the Optimize plugin via Google Tag Manager, it will often result in significantly increased latency

https://support.google.com/360suite/optimize/answer/7359264?hl=en

And they are not kidding. I was running an extremely basic test and this resulted in a very long lag before the site was rendered, as I used the JS that prevents the FOUC.

See backend here config;

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.