Jump to content

Need help understanding Google Analytics


Dirty Butter

Recommended Posts

Am I correct that the Google code needed to report sales to Analytics is in content.receipt.php? I use Paypal Standard on our 5.2.14 Blueprint store and our Kurouto 5.2.14 store.

 

Does the customer have to return to our store after payment to the receipt "thank you" page in order for Analytics to get information about the sale? Or does CC send the sale information to Google when the transaction is logged?

 

I ask, because I have never seen any conversion information on Google Analytics. What do I have to do to see sales information in relation to site visits? This is the first year we have spent for Google Adwords, so it's now important that I understand if those advertising dollars might be worthwhile or not.

Link to comment
Share on other sites

"Does the customer have to return to our store after payment to the receipt "thank you" page in order for Analytics to get information about the sale?"

 

That's how I see it. The GA code is all javascript and as such, must run in the browser. Who's browser? I suppose it does not matter -- if you can emulate a customer requesting an order's receipt page.

 

I suppose the same GA code could be added to the admin template orders.print.php, so that when you print the shipping manifest, the GA javascript will execute in your browser.

 

This assumes that, if GA receives info about an order more than once, regardless where it actually came from (IP address), the second and any later notification will be ignored. This is an assumption based on the logical reasoning of what should happen if a customer requests the receipt page more than once.

 

Do you want to try an experiment to add GA to the admin's shipping manifest?

Link to comment
Share on other sites

Actually, the template content.receipt.php is called when CubeCart determines the order is "complete" -- which is when the customer returns to your store from the payment gateway (on gateways where the customer never leaves the store, the "complete" phase is guaranteed). This is not when the customer asks for a receipt from their Accounts section.

 

I don't do GA, and so cannot test this, so you will need to be prepared to have mistakes in the code.

 

In the skin file content.receipt.php, find the GA code. It starts with {if $ANALYTICS}<script> and ends with </script>{/if}.

 

In the admin template orders.print.php, create a new blank line just above {/foreach}{/if}</body></html>.

 

Copy the GA code onto this new blank line.

 

Now we have to populate the skin variables $ANALYTICS, $GA_SUM, and $GA_ITEMS. We will borrow the code from CubeCart->_complete().

 

In the file admin/sources/orders.index.inc.php, find near line 467:

            $label_fix = array('ship_method','ship_tracking','gateway');
            foreach ($summary as $key => $value) {
                if (empty($value) && in_array($key,$label_fix)) $summary[$key] = '&nbsp;';
            }

Add above it, this:

            // Analytics
            if(in_array($summary['status'],array(2,3))) {
               $ga_id = $GLOBALS['config']->get('config', 'google_analytics');
               $ga_id = trim($ga_id);
               $GLOBALS['smarty']->assign('ANALYTICS', !empty($ga_id) ? $ga_id : false);
               $GLOBALS['smarty']->assign('GA_ITEMS', $inventory);
               $vars['ga_sum']    = $summary;
               $vars['ga_sum']['country_iso'] = getCountryFormat($summary['country'], 'numcode', 'iso');
               $vars['ga_sum']['google_id'] = $GLOBALS['config']->get('config', 'google_analytics');
               $vars['ga_sum']['store_name'] = $GLOBALS['config']->get('config', 'store_name');
               $GLOBALS['smarty']->assign('GA_SUM', $vars['ga_sum']);
            }

There is a hook available in CubeCart->_complete(), 'class.cubecart.construct.complete', which offers plugins and snippets to do some preliminary work prior to CubeCart collecting/organizing the data to be displayed in the order complete "receipt". I think not including this hook in this experiment will not be a problem.

Link to comment
Share on other sites

I think there also needs to be the javascript that creates the ga() function. So far, we have only copied the code that executes the ga() function for order conversions.

 

So, from the skin template file main.php, copy the code that starts with {if $ANALYTICS}{literal} and ends with {/literal}{/if}, to a place in the admin template orders.print.php just above the </head> statement as shown here:

    {if $ANALYTICS}
    {literal}<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','//www.google-analytics.com/analytics.js','ga');
ga('create', '{/literal}{$ANALYTICS}{literal}', 'auto');  // Replace with your property ID.
ga('send', 'pageview');
</script>{/literal}
    {/if}
  </head>

We do not need the 'pageview' command, so:

ga('send', 'pageview');

can probably be removed.
 

Link to comment
Share on other sites

 

Actually, the template content.receipt.php is called when CubeCart determines the order is "complete" -- which is when the customer returns to your store from the payment gateway (on gateways where the customer never leaves the store, the "complete" phase is guaranteed). This is not when the customer asks for a receipt from their Accounts section.

 

That's what I thought, as Standard PP does NOT send the customer back to CC, but only an inconspicuous link is provided to return.

 

I'll do some backing up and give your edits a try in a bit.

Link to comment
Share on other sites

Assuming nothing crashes when you select a few orders for printing and click "Go", you next wait a bit (to give GA headquarters some time to log the data) then check your GA account.

 

If you have a utility for your browser that tracks incoming and outgoing traffic, you may be able to spot if and when the javascript sends the data.

Link to comment
Share on other sites

No. There should be a GET request to www.google-analytics.com/analytics.js, followed by another line of traffic to google-analytics.com.

 

I now see a flaw in the edits to orders.print.php. There should be a set of data to send for each order, which means the code copied into it as instructed in post #4 needs to be inside the {foreach}. Otherwise, only the last order to be printed will have a notification sent to GA.

 

I will have some edits soon.

Link to comment
Share on other sites

Where I said this:

In the admin template orders.print.php, create a new blank line just above </body></html>.

 

Move the copied code just above this:

  {/foreach}
  {/if}
</body>
</html>

 

( I have changed the instruction in post #4.)


I believe orders.print.php is used whether you have an order be printed from that order's Order Summary screen, or from checked orders on the Orders List.

Link to comment
Share on other sites

In the admin template file orders.print.php, make this change:

Was new code added just above:
  {/foreach}
  {/if}
</body>
</html>
 
Now this code in its place:
{* NEW *}
  {if $ANALYTICS}
  <script>
    {literal}
    ga('require', 'ecommerce', 'ecommerce.js');
    ga('ecommerce:addTransaction', {
      'id': '{/literal}{$order.GA_SUM.cart_order_id}{literal}',
      'affiliation': '{/literal}{$order.GA_SUM.store_name}{literal}',
      'revenue': '{/literal}{$order.GA_SUM.total}{literal}',
      'shipping': '{/literal}{$order.GA_SUM.shipping}{literal}',
      'tax': '{/literal}{$order.GA_SUM.total_tax}{literal}'
    });
    {/literal}
    
    {foreach from=$order.GA_ITEMS item=item}
      {literal}
      ga('ecommerce:addItem', {
          'id': '{/literal}{$order.GA_SUM.cart_order_id}{literal}',
          'name': '{/literal}{$item.name}{literal}',
          'sku': '{/literal}{$item.product_code}{literal}',
          'category': '{/literal}{if isset($item.options)}{foreach from=$item.options item=option}{$option} {/foreach}{/if}{literal}',
          'price': '{/literal}{$item.price}{literal}',
          'quantity': '{/literal}{$item.quantity}{literal}'
        });
      {/literal}
    {/foreach}
    ga('ecommerce:send');
    </script>
  {/if}
{* NEW *}

We need to be having the GA data be an element of each $order.

 

In the file admin/sources/orders.index.inc.php:

Recently added code:
            // Analytics
            if(in_array($summary['status'],array(2,3))) {
               $ga_id = $GLOBALS['config']->get('config', 'google_analytics');
               $ga_id = trim($ga_id);
               $GLOBALS['smarty']->assign('ANALYTICS', !empty($ga_id) ? $ga_id : false);
               $GLOBALS['smarty']->assign('GA_ITEMS', $inventory);
               $vars['ga_sum']    = $summary;
               $vars['ga_sum']['country_iso'] = getCountryFormat($summary['country'], 'numcode', 'iso');
               $vars['ga_sum']['google_id'] = $GLOBALS['config']->get('config', 'google_analytics');
               $vars['ga_sum']['store_name'] = $GLOBALS['config']->get('config', 'store_name');
               $GLOBALS['smarty']->assign('GA_SUM', $vars['ga_sum']);
            }
 
Replace the above recently added code with the below code:
/* NEW */
            // Analytics
            if(in_array($summary['status'],array(2,3))) {
               $ga_id = $GLOBALS['config']->get('config', 'google_analytics');
               $ga_id = trim($ga_id);
               $vars['ga_sum']    = $summary;
               $vars['ga_sum']['country_iso'] = getCountryFormat($summary['country'], 'numcode', 'iso');
               $vars['ga_sum']['google_id'] = $GLOBALS['config']->get('config', 'google_analytics');
               $vars['ga_sum']['store_name'] = $GLOBALS['config']->get('config', 'store_name');

               $GLOBALS['smarty']->assign('ANALYTICS', !empty($ga_id) ? $ga_id : false);
               $summary['GA_ITEMS'] = $inventory; //$GLOBALS['smarty']->assign('GA_ITEMS', $inventory);
               $summary['GA_SUM']   = $vars['ga_sum']; // $GLOBALS['smarty']->assign('GA_SUM', $vars['ga_sum']);
            }
/* NEW */

Here, the GA_ITEMS and GA_SUM data become an element of the respective order summary we are collecting the data for.

 

Relevant parts of the data that I see on two GET requests on the Network tab of my browser:

URL: http://www.google-analytics.com/r/collect
QUERYSTRING (parts):
t=transaction
_s=1
dt=Order%20Invoice%20-%20141210-103146-3640
ti=141210-103146-3640
ta=Graystone%20Industries
tr=%2414.67
ts=%246.66
tt=%241.02
 
URL: http://www.google-analytics.com/r/collect
QUERYSTRING (parts):
t=item
_s=2
dt=Order%20Invoice%20-%20141210-103146-3640
tid=1
ti=141210-103146-3640
in=Test%20Product%20%234
ic=TESA34
ip=6.99
Link to comment
Share on other sites

I really appreciate all your efforts to help and educate me, but even with your new code I'm not getting any analytics javascript on NET.

 

I did find a conversion code that could be added that shows Conversions straight to Google Adwords. I don't know if it works yet, but it DOES show up in the Javascript tab of NET when I click the Print icon on Order Summary. So I know what an analytics link looks like in NET.

 

I just installed a Google Tag Assistant in Chrome, and it may be a browser addin that is messing me up. Will try again tomorrow.

Link to comment
Share on other sites

I had to use the GA code straight from Google to get the analytics.js to show in NET on the Blueprint customer product page. But it does work with the $ANALYTICS on the Kurouto skin for some reason.

 

But I still don't get analytics.js showing on NET for the Print page. So I must be confused:

 

1. Copied the {if $ANALYTICS} section from main.php to above /head in orders.print.php

2. Copied NEW section from #17 to just above {/foreach}

3. Copied NEW section from #17 to orders.index.inc.php just above

$label_fix = array('ship_method','ship_tracking','gateway');
Also, I noticed in your example of what the NET headers showed that you had other information besides the product link - I don't get price, etc., on the NET analytics.js  Request Header for the product page.

Accept

blushing.gif

Accept-Encoding

gzip, deflate

Accept-Language

en-US,en;q=0.5

Cache-Control

no-cache

Connection

keep-alive

DNT

1

Host

www.google-analytics.com

Pragma

no-cache

Referer

http://dirtybutter.com/plushcatalog/gund-heads-and-tales-lg-white-bear-red-green-plaid-bow.html

User-Agent

Mozilla/5.0 (Windows NT 6.3; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0

I get the same kind of info on the Kurouto store as well.

I won't have time to work on this anymore until company has gone home after Christmas. I hope you and your family have some wonderful time spent together. This can certainly wait!!!

Link to comment
Share on other sites

Referring to post #4 and #17, in file admin/sources/orders.index.inc.php, I asked you add code just above:

Find near line 467:
            $label_fix = array('ship_method','ship_tracking','gateway');
            foreach ($summary as $key => $value) {
                if (empty($value) && in_array($key,$label_fix)) $summary[$key] = '&nbsp;';
            }

I see that there two occurrences of these lines of code:

Near line 222, find:
if (in_array($_GET['action'], array('add', 'edit'))) {
then those lines are found about 76 lines after that.

But it's the second occurrence that the code needs added:

Near line 415, find:
} else if (isset($_GET['print']) && !empty($_GET['print'])) {
then those lines are found about 52 lines later.

The line numbers refer to an unedited CC5214, so if your files have any other customization, be very attentive of where to add the code.

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...