AbanteCart Community

Shopping Cart Operations => Support => General Support => Topic started by: Shenhav on September 05, 2012, 09:15:31 PM

Title: Google Analytics
Post by: Shenhav on September 05, 2012, 09:15:31 PM
Hi All,

I want to add google analytics to my website, I need to paste google code before the closing </head> tag on every page.
Where can I find it.

Thanks
Title: Re: Google Analytics
Post by: abantecart on September 06, 2012, 08:14:15 AM
If you need add generic code (show on all pages) into storefront head you can do this 2 ways.

1. Add code to TPL file /storefront/view/default/template/common/head.tpl
This whole file is pulled into <head></head>

2. You can include child block or just code into controller /storefront/controller/common/head.php
This can be done by adding (concatenating) to variable $head

If you need to add some script or CSS to head in some specific page only, you can use addScript or addStyle in that page controller.
These methods will append your script or CSS to the head on on that page.
Example:
Code: [Select]
$this->document->addScript(RDIR_TEMPLATE . 'javascript/jqgrid/js/jquery.jqGrid.min.js');
Code: [Select]
           $this->document->addStyle(array(
                'href' => $this->view->templateResource('/javascript/jquery/star-rating/jquery.rating.css'),
                'rel' => 'stylesheet',
                'media' => 'screen',
            ));

Title: Re: Google Analytics
Post by: Nimitz1061 on September 10, 2012, 10:48:11 AM
I'm a bit more concerned with how to add ecommerce transaction tracking code.   Any pointers there?  Ideal outcome would be template agnostic, as it is obviously desirable to have tracking work in all themes...

David
Title: Re: Google Analytics
Post by: abantecart on September 10, 2012, 09:37:24 PM
Solution #2 was for controller, so it is multi-template solution and will work on any template.
Title: Re: Google Analytics
Post by: Nimitz1061 on September 11, 2012, 12:31:14 PM
Both of those seem to presume you're just importing a file which contains the Javascript.  Doesn't seem to address the need to compose portions of the script on the fly....

David
Title: Re: Google Analytics
Post by: Nimitz1061 on September 11, 2012, 03:15:27 PM
Hmm.  Seems I've discussed this before - need to split that script and get part of it into global foot...

David
Title: Re: Google Analytics
Post by: Nimitz1061 on September 12, 2012, 10:24:59 AM
Indeed, I have.  See this post on asynchronous scripts (http://forum.abantecart.com/index.php/topic,226.msg783.html#msg783)

David
Title: Re: Google Analytics
Post by: Nimitz1061 on September 12, 2012, 12:22:24 PM
More specifically to Google Analytics implementation, GA requires the insertion of 2 OR MORE basic script elements.  All pages should have a  top script element, which includes some basic setup information such as the GA Web Property ID, and some configuration values for account usage (Cross Domain tracking, that sort of thing), and a bottom script element, which pulls the analytics javascript file from Google and executes the actual data insertion.

In between may be one or more "pushes".  This must include at least one on the checkout success page in order to achieve full ecommerce transaction tracking.   So, at least 3 script insertions must be done in order to get adequate tracking in place. 

One possible barrier to getting this done will be the checkout success page - which seems to immediately clear any existing order data......

David
Title: Re: Google Analytics
Post by: Nimitz1061 on October 04, 2012, 08:42:14 AM
As the checkout success page is not changed in the 1.1.0 release, it would appear that the data needed to accurately emit transactional data remains missing from that page.

This does need to be addressed, quickly, and in the core code.
Title: Re: Google Analytics
Post by: abolabo on October 05, 2012, 07:19:42 AM
You need hook storefront/controller/pages/checkout/confirm.php
Something like this:
Code: [Select]
public function onControllerPagesCheckoutConfirm_UpdateData() {
$registry = Registry::getInstance();
$order_id = $registry->get('session')->data['order_id'];
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);
$script_html = "Place Some html Here, for ex: <script> alert('Order Total: ".$order_info['total']."')</script>";
$this->baseObject->view->addHookVar('payment_pre',$script_html);
return ;
}

Also look inside your confirm.tpl and check where do you plan place your hook-var. If you makes your own template you can create your own hookvar with any name and then assign value in hook.
As you see i used hookvar "payment_pre" of our default template in example above.
Title: Re: Google Analytics
Post by: Nimitz1061 on October 05, 2012, 10:00:47 AM
Abolabo,

This looks more promising.  I'm concerned about the activity taking place on the confirm action, and using a payment_pre hook.

The whole idea of the tracking is to catch successful orders for which payment has been received.  This looks to me to be happening in the confirmation page, and before the transaction is complete.

Is this the case?

Oh, and from where can I hook this?

I've been trying to encode this from extensions/strikehawk_ganalytics/core  but that does not seem to work.

Thanks,

David

Title: Re: Google Analytics
Post by: abantecart on October 05, 2012, 02:25:11 PM
If you need to hook after the order is created and the transaction is complete you need to hook similar way to ControllerPagesCheckoutSuccess  controller.  This one is call after order is all complete.

You put it into your extension core file class  extensions/strikehawk_ganalytics/core/strikehawk_ganalytics.php

class ExtensionStrikehawk_ganalytics extends Extension {
   
....

Title: Re: Google Analytics
Post by: Nimitz1061 on October 21, 2012, 09:14:44 AM
I'm under the impression that ControllerPagesCheckoutSuccess only knows that checkout is complete because it has been called. (routed to, whatever).

Are you telling me that the :

Code: [Select]
if (isset($this->session->data['order_id'])) {

conditional is a check for order completion ??

David
Title: Re: Google Analytics
Post by: Nimitz1061 on October 21, 2012, 11:28:47 AM
Ok, I put :

Code: [Select]
public function onControllerPagesCheckoutConfirm_UpdateData() {
$registry = Registry::getInstance();
$order_id = $registry->get('session')->data['order_id'];
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);
$script_html = "Place Some html Here, for ex: <script> alert('Order Total: ".$order_info['total']."')</script>";
$this->baseObject->view->addHookVar('payment_pre',$script_html);
return ;
}

in extensions/strikehawk_ganalytics/core/strikehawk_ganalytics.php file as:

Code: [Select]
class StrikehawkGanalyticsStrikehawkGanalytics extends Extension {
public function __construct(){
  $this->registry = Registry::getInstance();
}

public function onControllerPagesCheckoutConfirm_UpdateData() {
$registry = Registry::getInstance();
$order_id = $registry->get('session')->data['order_id'];
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);
$script_html = "Place Some html Here, for ex: <script> alert('Order Total: ".$order_info['total']."')</script>";
$this->baseObject->view->addHookVar('payment_pre',$script_html);
return ;
}



Placed:
Code: [Select]
<?php echo $this->getHookVar('payment_pre'); ?>in:

storefront/view/default/template/pages/checkout/confirm.php

Nothing appearing.  Adding text above the getHookVar call does result in that text showing up on the page, so I think I have the right template for this purpose.  (Still would rather this comes out on checkout success, but at this point am just trying to get something to display in a catalog page...)

Now, should note I have no controllers or models setup for this extension. 

Title: Re: Google Analytics
Post by: abolabo on October 22, 2012, 05:43:11 AM
Ok, I put :

Code: [Select]
public function onControllerPagesCheckoutConfirm_UpdateData() {
$registry = Registry::getInstance();
$order_id = $registry->get('session')->data['order_id'];
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);
$script_html = "Place Some html Here, for ex: <script> alert('Order Total: ".$order_info['total']."')</script>";
$this->baseObject->view->addHookVar('payment_pre',$script_html);
return ;
}

in extensions/strikehawk_ganalytics/core/strikehawk_ganalytics.php file as:

Code: [Select]
class StrikehawkGanalyticsStrikehawkGanalytics extends Extension {
public function __construct(){
  $this->registry = Registry::getInstance();
}

public function onControllerPagesCheckoutConfirm_UpdateData() {
$registry = Registry::getInstance();
$order_id = $registry->get('session')->data['order_id'];
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);
$script_html = "Place Some html Here, for ex: <script> alert('Order Total: ".$order_info['total']."')</script>";
$this->baseObject->view->addHookVar('payment_pre',$script_html);
return ;
}



Placed:
Code: [Select]
<?php echo $this->getHookVar('payment_pre'); ?>in:

storefront/view/default/template/pages/checkout/confirm.php

Nothing appearing.  Adding text above the getHookVar call does result in that text showing up on the page, so I think I have the right template for this purpose.  (Still would rather this comes out on checkout success, but at this point am just trying to get something to display in a catalog page...)

Now, should note I have no controllers or models setup for this extension.
Hello.
1. code works 100%. i tested.
2. check is your core/strikehawk_ganalytics.php file included in main.php. Open main.php of your extension, there are have to be something like this
Code: [Select]
if(!class_exists('StrikehawkGanalyticsStrikehawkGanalytics')){
include('core/strikehawk_ganalytics.php');
}
3. check is file run via var_dump('bla-bla-bla'); If not - check status of extension.
Title: Re: Google Analytics
Post by: Nimitz1061 on October 22, 2012, 10:02:09 AM
Those lines exist between lines 10 and 12 of the extension generator created code.

Inserting a
Code: [Select]
die() in to the core/strikehawk_ganalytics.php file does cause the script to die.

So, the issue is somewhere else.

David
Title: Re: Google Analytics
Post by: abolabo on October 25, 2012, 06:16:26 PM
try this
Code: [Select]
class ExtensionStrikehawkGanalytics extends Extension {

    public function onControllerPagesCheckoutConfirm_UpdateData() {
        $registry = Registry::getInstance();
        $order_id = $registry->get('session')->data['order_id'];
        $order_info = $registry->get('model_checkout_order')->getOrder($order_id);
        $webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid');
        $script_html = "Place Some html Here, for ex: <script> alert('Order Total: " . $order_info['total'] . "')</script>";
        $this->baseObject->view->addHookVar('payment_pre', $script_html);
        return;
    }

    public function onControllerCommonHead_UpdateData() {
        $registry = Registry::getInstance();
        $registry->get('document')->addScript($this->baseObject->view->templateResource('/javascript/global_scripts/myscript.js'));

        $this->baseObject->view->addHookVar('another_var_name', 'Confirmation Massage');
    }
}
Title: Re: Google Analytics
Post by: Nimitz1061 on November 03, 2012, 10:01:40 AM
abolabo

That last example worked, after I changed the include call in the main.php file to match the class name - to the extent that the confirmation message now appears on the screen.

That left a PHP error on the config value call - which I managed to resolve using another example in the documentation.  More specifically, I changed from:

Code: [Select]
$webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid'); to:

Code: [Select]
$webpropertyid = $this->registry->get('config')->get('strikehawk_ganalytics_webpropertyid');
Implementing a similar method for the footer controller with an additional hook var display in the footer.tpl file also worked.

At the moment, I am now able to generate the script markup for the head and foot, and am now working on the transaction script output.

This is a very nice piece of progress, which I could not have done without your help.

Thanks!

David

Title: Re: Google Analytics
Post by: Nimitz1061 on November 04, 2012, 10:13:55 AM
I'm now working on generating transactional data on the checkout SUCCESS page.

This location is important.  I've mentioned a number of times throughout this process that analytic software should only report success after the confirmation is received and the payment processed.

I'm currently using:

Code: [Select]
    public function onControllerPagesCheckoutSuccess_InitData() {
        $registry = Registry::getInstance();
        $script_html = 'SCRIPT TAG';
        if (isset($this->session->data['order_id'])) {
        $order_id = $registry->get('session')->data['order_id'];
        $order_info = $registry->get('model_checkout_order')->getOrder($order_id);
        $webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid');
        $script_html = "Place Some html Here, for ex: <script> alert('Order Total: " . $order_info['total'] . "')</script>";
        }
        $this->baseObject->view->addHookVar('payment_analytics', $script_html);
        return;
    }

as opposed to the suggested:

Code: [Select]
    public function onControllerPagesCheckoutConfirm_UpdateData() {
        $registry = Registry::getInstance();
        $order_id = $registry->get('session')->data['order_id'];
        $order_info = $registry->get('model_checkout_order')->getOrder($order_id);
        $webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid');
        $script_html = "Place Some html Here, for ex: <script> alert('Order Total: " . $order_info['total'] . "')</script>";
        $this->baseObject->view->addHookVar('payment_analytics', $script_html);
        return;
    }

In either case, I'm getting an error similar to this:

Fatal error: Call to a member function getOrder() on a non-object in extensions/strikehawk_ganalytics/core/strikehawk_ganalytics.php on line 27

which refers to the line:

Code: [Select]
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);

My diagnostic code (echoing the $order_id variable) indicates that I do have a valid order ID in the session.  So, it appears that this is not the correct way to invoke the checkout order model...

David
Title: Re: Google Analytics
Post by: abolabo on November 04, 2012, 05:26:08 PM
I'm now working on generating transactional data on the checkout SUCCESS page.

This location is important.  I've mentioned a number of times throughout this process that analytic software should only report success after the confirmation is received and the payment processed.

I'm currently using:

Code: [Select]
    public function onControllerPagesCheckoutSuccess_InitData() {
        $registry = Registry::getInstance();
        $script_html = 'SCRIPT TAG';
        if (isset($this->session->data['order_id'])) {
        $order_id = $registry->get('session')->data['order_id'];
        $order_info = $registry->get('model_checkout_order')->getOrder($order_id);
        $webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid');
        $script_html = "Place Some html Here, for ex: <script> alert('Order Total: " . $order_info['total'] . "')</script>";
        }
        $this->baseObject->view->addHookVar('payment_analytics', $script_html);
        return;
    }

as opposed to the suggested:

Code: [Select]
    public function onControllerPagesCheckoutConfirm_UpdateData() {
        $registry = Registry::getInstance();
        $order_id = $registry->get('session')->data['order_id'];
        $order_info = $registry->get('model_checkout_order')->getOrder($order_id);
        $webpropertyid = $this->baseObject->get('config')->get('strikehawk_ganalytics_webpropertyid');
        $script_html = "Place Some html Here, for ex: <script> alert('Order Total: " . $order_info['total'] . "')</script>";
        $this->baseObject->view->addHookVar('payment_analytics', $script_html);
        return;
    }

In either case, I'm getting an error similar to this:

Fatal error: Call to a member function getOrder() on a non-object in extensions/strikehawk_ganalytics/core/strikehawk_ganalytics.php on line 27

which refers to the line:

Code: [Select]
$order_info = $registry->get('model_checkout_order')->getOrder($order_id);

My diagnostic code (echoing the $order_id variable) indicates that I do have a valid order ID in the session.  So, it appears that this is not the correct way to invoke the checkout order model...

David
hi.
i think you doing right. (i mean hook on success page)
about error.
i guess cause is model_checkout_order var. Try to paste
Code: [Select]
$this->baseObject->loadModel('checkout/order'); or
Code: [Select]
$registry->get('load')->model('checkout/order');before getOrder call.
Title: Re: Google Analytics
Post by: Nimitz1061 on November 04, 2012, 06:44:48 PM
It appears that
Code: [Select]
$this->baseObject->loadModel('checkout/order'); works.  Not sure why, as the baseObject type calls have been failing earlier..

Now to look at how to obtain itemized product data....

Thanks!

David
Title: Re: Google Analytics
Post by: Nimitz1061 on November 05, 2012, 07:00:22 AM
One thing I am noticing is that I have products reappearing in the cart after I log in to complete the next checkout...

Title: Re: Google Analytics
Post by: abolabo on November 05, 2012, 09:48:26 AM
You don't clear session data after checkout.
 
Look into file storefront/controller/pages/checkout/success.php

Code: [Select]
class ControllerPagesCheckoutSuccess extends AController {
public function main() {

        //init controller data
        $this->extensions->hk_InitData($this,__FUNCTION__);

if (isset($this->session->data['order_id'])) {
$this->cart->clear();

unset($this->session->data['shipping_method']);
unset($this->session->data['shipping_methods']);
unset($this->session->data['payment_method']);
unset($this->session->data['payment_methods']);
unset($this->session->data['guest']);
unset($this->session->data['comment']);
unset($this->session->data['order_id']);
unset($this->session->data['coupon']);
}
   

Title: Re: Google Analytics
Post by: Nimitz1061 on November 06, 2012, 08:41:05 AM
Thanks,  I'll check there.

Odd thing is, now that I have logged out once, it appears to have stopped happening...

Now working on accessing various elements of the order and cart....

David
Title: Re: Google Analytics
Post by: FG on February 17, 2013, 12:38:28 PM
This seems to be a lot of work to get Google Analytics on your site.  In other shopping carts you just paste your Google Analytics code in to a box and press save - is this going to be a feature in future release (please)?  :)
Title: Re: Google Analytics
Post by: Nimitz1061 on February 20, 2013, 03:16:41 AM
This seems to be a lot of work to get Google Analytics on your site.  In other shopping carts you just paste your Google Analytics code in to a box and press save - is this going to be a feature in future release (please)?  :)


Actually, getting Google Analytics to "work" on your site is just that easy in ANY shopping cart, including this one.  If you are willing to accept a low enough value of "works".

I  (and most of my clients) expect a bit more than just counting clicks and generating some page statistics.  This is after all, an eCommerce application - so ecommerce tracking should be the minimum standard.  That takes more work on every cart I've seen.

Another factor is that not everyone chooses Google Analytics to track their site performance and behaviors.   Our clients also use Piwik, Channel Advisor and other tracking systems.  So, what the cart should have, as standard, is an analytics framework.  Which is what I've been working towards.   

David
Title: Re: Google Analytics
Post by: sowerswebtech on November 04, 2015, 01:36:28 PM
Hi All,

I want to add google analytics to my website, I need to paste google code before the closing </head> tag on every page.
Where can I find it.

Thanks

I found this great tutorial on how to get Google Analytics easily installed within your installation of AbanteCart. SUPER easy.
fastcomet.com/tutorials/abantecart/google-analytics
Title: Re: Google Analytics
Post by: Basara on November 05, 2015, 01:22:54 AM
Thank you, sowerswebtech