Jump to content

Google Analytics


steve.rolfe@mac.com

Recommended Posts

Hi,

I am trying to configure Google Analytics for my site.  I've put in the Analytics ID number in the Store administrator settings some time ago, but it has never worked - nothing appears on the google console.   I haven't however put in any tracking code as I wasn't sure where to put it.  I see that it is supposed to go in the head section of every page.  I've searched the forum and others have said put it in main.php - however which one?   I have tried putting it in as the first entry under my templates/foundation main.php and all it does is prevent from the page from loading.

Is someone able to assist as to correctly setup analytics for ALL of the pages on the website please?

thanks in advance.  This is the code I need to put in:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11031594-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-11031594-1');
</script>
 

Link to comment
Share on other sites

Currently, the Foundation skin template main.php has this statement at line 21:

{include file='templates/element.google_analytics.php'}

Looking in the template element.google_analytics.php, we see that (and this has been the case for some months now) the visitor must have agreed to the use of cookies in order for GA to work. You must also not have configured your browser add-ons (no-script, cookie-basher, etc) to whitelist Google's cookies and javascript.

Notice also the use of {literal} and {/literal}. The Smarty template engine reserves for itself the use of braces to indicate what is and what is not Smarty code. So, if any code in a template has braces, it must follow a syntax rule, or be enclosed in these tags. The syntax rule is that braces must have whitespace next to them. Whitespace is a space, new line, or tab.

Looking at your code above, we see that line 5 has braces with a character other than whitespace right next to the brace character. This is a syntax error and will cause Smarty to throw an exception (which CubeCart does not yet catch).

The GA code template is included in the main.php, main.checkout.php, and main.stream.php templates, which covers all pages.

If you really need to use your version of the GA code, you can create your own version of element.google_analytics.php, after fixing the syntax error, or wrapping your code in {literal} tags.

Link to comment
Share on other sites

thank you for the reply - not quite sure I understand it though :(

I don't really mind if i use that code or not, all i know is that the analytics isn't working and hasn't worked with cubecart since day one of our install (i only just found out today).  I have have done is put our code in the area within store administration as that is all I thought we needed to do to get it working?

What else could then be stopping it working - assuming I am not required to put any code in manually - just the analytics userid into the admin area please?

Link to comment
Share on other sites

I am not seeing any of the GA javascript code in the page.

We would like to know the exact version of CubeCart you are using, and if you have kept an older version of Foundation as the primary skin to use.

View the contents of main.php to determine if there is this near line 21:

{include file='templates/element.google_analytics.php'}

Then, determine if there is a template file named element.google_analytics.php, and if so, view the contents of that file. Is the first line this?

{if isset($smarty.cookies.accept_cookies) && $smarty.cookies.accept_cookies=='true'}

If your site is being hosted, the hosting provider may have given you a control panel to access your account settings. There will be a file viewer in that control panel you can use to view these files.

Link to comment
Share on other sites

thanks for the reply:

CubeCart Version
6.4.4
 
Yes, there is:

<title>{$META_TITLE}</title>
      {include file='templates/element.meta.php'}
      <link href="{$CANONICAL}" rel="canonical">
      <link href="{$ROOT_PATH}favicon.ico" rel="shortcut icon" type="image/x-icon">
      {include file='templates/element.css.php'}
      {include file='templates/content.recaptcha.head.php'}
      {include file='templates/element.google_analytics.php'}
      {include file='templates/element.js_head.php'}
   </head>
   <body>


Yes, that file is there - :

 

{if isset($smarty.cookies.accept_cookies) && $smarty.cookies.accept_cookies=='true'}
{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');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>{/literal}
{/if}

 

Link to comment
Share on other sites

Please verify something: In admin, Store Settings, Extra tab, GDPR section, is the "Cookie Compliance Dialogue" checkbox checked?

I am thinking that the only way to get the GA code to appear is for $smarty.cookies.accept_cookies to be true, which means there must be an actual cookie named 'accept_cookies', and the only way to get that cookie is to display the "We use cookies" dialog window and click on the 'Accept' button , and the only way to display that dialog window is to enable this store setting.

 

Link to comment
Share on other sites

  • 6 months later...
  • 1 month later...

Hi,

Problem is that cubecart is using gtag to send info for ecommerce data and only info are sent from order complete page :

{if isset($smarty.cookies.accept_cookies) && $smarty.cookies.accept_cookies=='true' && $ANALYTICS && $GA_SUM}
<script>
{literal}(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');
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
    'id': '{/literal}{$GA_SUM.cart_order_id}{literal}',
    'affiliation': '{/literal}{$GA_SUM.store_name}{literal}',
    'revenue': '{/literal}{$GA_SUM.total}{literal}',
    'shipping': '{/literal}{$GA_SUM.shipping}{literal}',
    'tax': '{/literal}{$GA_SUM.total_tax}{literal}'
  });
{/literal}{foreach from=$GA_ITEMS item=item}{literal}ga('ecommerce:addItem', {
    'id': '{/literal}{$GA_SUM.cart_order_id}{literal}',
    'name': '{/literal}{$item.name}{literal}',
    'sku': '{/literal}{$item.product_code}{literal}',
    'price': '{/literal}{$item.price}{literal}',
    'quantity': '{/literal}{$item.quantity}{literal}',
    'category': '{/literal}{$ITEM_CATS.{$item.product_id}}{literal}'
  });{/literal}{/foreach}{literal}  ga('ecommerce:send');{/literal}
</script>

, universal analytics will be discontinued by end of the year and new GA4 will take place, now in order to capture any data for it, is to set up datalayers which need to be coded to website by dev and set up via GTM. I have asked if anyone had done this already. 

For user flow etc. : item view, add to cart, checkout started, purchased. 

it would be great if someone has already implemented this or if cubecart devs are planning to add this into code in a future.

 

On 7/14/2021 at 1:55 AM, [email protected] said:

Hi,

I am trying to configure Google Analytics for my site.  I've put in the Analytics ID number in the Store administrator settings some time ago, but it has never worked - nothing appears on the google console.   I haven't however put in any tracking code as I wasn't sure where to put it.  I see that it is supposed to go in the head section of every page.  I've searched the forum and others have said put it in main.php - however which one?   I have tried putting it in as the first entry under my templates/foundation main.php and all it does is prevent from the page from loading.

Is someone able to assist as to correctly setup analytics for ALL of the pages on the website please?

thanks in advance.  This is the code I need to put in:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11031594-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-11031594-1');
</script>
 

add red highlighted to your code, replace all within the file in templates/element.google_analytics.php

{if isset($smarty.cookies.accept_cookies) && $smarty.cookies.accept_cookies=='true'}
<!-- Global site tag (gtag.js) - Google Analytics -->

{literal}
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11031594-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-11031594-1');
</script>
{/literal}
{/if}

Link to comment
Share on other sites

  • 4 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...