Jump to content

Google Analytics


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

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.

 Share

×
×
  • Create New...