[SOLVED] Google Analytics - New Version Causing Problems

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

[SOLVED] Google Analytics - New Version Causing Problems

Post by govicinity »

CMSMS 1.11.10
AceEditor 1.0
AdvancedContent 0.9.4.3
CGExtensions 1.38.6
CGSimpleSmarty 1.7.2
CGSmartImage 1.16
ListIt2 1.4.1
SiteMapMadeSimple 1.2.8

I currently use this code on the homepage template for my sites

Code: Select all

{content block='Google_Analytics' label='Google Analytics - if you have Google Analytics code add it here, including the start <__script__> and end </__script> tags' assign='Google_Analytics' wysiwyg='false'}
{if !empty($Google_Analytics)}
<!-- Start Google Analytics -->
{$Google_Analytics}
<!-- End Google Analytics -->
{/if}
In the <head> section, then on the rest of the pages within my sites I use a GCB called "google_analytics_repeat" by inserting this smarty {global_content name='google_analytics_repeat'}, within this GCB I have this code

Code: Select all

{$cgsimple->get_page_content('home','Google_Analytics','foo')}{eval var=$foo}
All has been working really well until Google started using "{}" curly brackets in their tracking code. Now I know that I can add {literal} tags to my the block='Google_Analytics', but I don't want to do that, as this block only appears if content is added to it - and I don't want my clients to see the literal tags... I'm trying to make this as foolproof for them in the admin area as possible - so they don't delete things by mistake.

Is there any way for me to sort this easily? Any help gratefully received.
Last edited by govicinity on Mon Apr 14, 2014 9:16 am, edited 1 time in total.
Going up, woop, woop.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Google Analytics - New Version Causing Problems

Post by calguy1000 »

I would take out the {eval} as the GA code is just javascript, and doesn't need to be passed through smarty.

so {$foo} instead of {eval var=$foo}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

Hi Calguy, thanks for the very swift response, I have removed that from the GCB, and that makes it perfect on all pages other than the homepage when I remove the {literal} tags, but where we have the initial

Code: Select all

<__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', 'UA-XXXXXXX-1', 'xxxxxxxx.com');
  ga('send', 'pageview');
</__script>
code pasted in the admin side on the homepage in the content block='Google_Analytics' I still get an oops/broken site - just the homepage tough, all internal pages are perfect now.

Error:
Error: at line 3 in file /var/www/web/lib/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php:

Message:

Syntax error in template "content:Google_Analytics" on line 3 "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" - Unexpected "[", expected one of: "}"

This happens when I take the {literal} tags out of the block='Google_Analytics'. Any ideas?
Going up, woop, woop.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Google Analytics - New Version Causing Problems

Post by calguy1000 »

Remove the if statement from the homepage template, and just call the same GCB.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

Here's what I have now

Code: Select all

<!-- Start Google Analytics -->
{content block='Google_Analytics' label='Google Analytics - if you have Google Analytics code add it here, including the start <__script__> and end </__script> tags' wysiwyg='false'}
<!-- End Google Analytics -->
Still doesn't like it unfortunately:

Error: at line 2 in file /var/www/web/lib/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php:

Message:

Syntax error in template "content:Google_Analytics" on line 2 "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){" - Unexpected "[", expected one of: "

Still works if I wrap the GA in {literal} tags in the block='Google_Analytics' again though! Dang.
Going up, woop, woop.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Google Analytics - New Version Causing Problems

Post by velden »

Wrap the {$Google_Analytics} in homepage template in {literal} tags.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: Google Analytics - New Version Causing Problems

Post by Rolf »

Add a space before and after all { and } brackets in the GA code, then no need for literal tags...
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Google Analytics - New Version Causing Problems

Post by velden »

Rolf wrote:Add a space before and after all { and } brackets in the GA code, then no need for literal tags...
True, but topic starter wants it to be as fool proof as possible for editors.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

@ Velden,
Thanks for the reply, I already tried this:

Code: Select all

{content block='Google_Analytics' label='Google Analytics - if you have Google Analytics code add it here, including the start <__script__> and end </__script> tags' assign='Google_Analytics' wysiwyg='false'}
{if !empty($Google_Analytics)}
<!-- Start Google Analytics -->
{literal}{$Google_Analytics}{/literal}
<!-- End Google Analytics -->
{/if}
If we do this then we just get "{$Google_Analytics}" printed on the actual page rather than it importing the code in the GCB into the page. Sorry!
Going up, woop, woop.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

@ Rolf,
Thanks for the suggestion, certainly a good one, but for the users of the CMS once it has been released to the client it needs to be as simple as possible, they will just get the code from google analytics and add that, I don't want them to have to edit the GA code afterwards. Currently I have in the client editable admin side (in the page admin) literal tags wrapping the GA code script, but I can't expect clients to know how to do this themselves as they have no knowledge of smarty, and some of them no knowledge of HTML at all, so I'm trying to make it totally foolproof for them. I know it's a bit of a head-scratcher, been trying to work it out for a couple of weeks now, and finally had to relent and ask for help from the wider CMSMS-brain.
Going up, woop, woop.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1968
Joined: Mon Jan 29, 2007 4:47 pm

Re: Google Analytics - New Version Causing Problems

Post by Jo Morg »

Of course... anything between the literal tags won't be parsed by Smarty so {literal}{$Google_Analytics}{/literal} won't work... :D
I don't think there is an easy way to do it. Rolf's suggestion seems the best for the moment.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: Google Analytics - New Version Causing Problems

Post by velden »

Jo Morg wrote:Of course... anything between the literal tags won't be parsed by Smarty so {literal}{$Google_Analytics}{/literal} won't work... :D
yeah yeah, I already feel stupid...

Would it be an option to ask the editor only for the 'UA-XXXXXXX-1'?

Then you can build the rest of the Google analytics code yourself. I assume those code is generic except for the UA-code and the domain which you already know.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

@Velden,
LOL, I realised that after I tried it the first time... any suggestions welcome though! Willing to try anything.

The GA code was generic up until the latest update, it now also includes your website's URL: ('create', 'UA-XXXXXXX-1', 'xxxxxxxx.com').

I know there used to be a plug in for this, but I think it's now stale in the Forge, I just wanted to create something that was easy to use and would not need to be a plugin. It used to work wonderfully before google updated their script!
Going up, woop, woop.
LadyHLG
Forum Members
Forum Members
Posts: 38
Joined: Wed Apr 21, 2010 1:47 pm

Re: Google Analytics - New Version Causing Problems

Post by LadyHLG »

This is actually a nice solution for setting up sites for smaller clients that want Universal GA, but who arent very tech savy and dont want to mess with too many other areas of the admin.

So following along from Velden's post it would be add the following to the home page template:

Code: Select all

{content block='Google_Analytics' oneline=true label='Google Analytics Code' assign='Google_Analytics' wysiwyg='false'}
So that all the client enters is their UA code.
Then in a GCB you have:

Code: Select all

{$cgsimple->get_page_content('blah','Google_Analytics','foo')}
{if !empty($foo)}
{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}{$foo}', '{$smarty.server.HTTP_HOST}{literal}');
  ga('send', 'pageview');
</__script>{/literal}
{/if}
I'm using smarty {$smarty.server.HTTP_HOST} to get the domain but you could just fill it in.
Finally, in all the templates, including the home template, you add the call to the GCB:

Code: Select all

{global_content name='google_analytics_repeat'}
Did I miss anything?
LadyHLG
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: Google Analytics - New Version Causing Problems

Post by govicinity »

Many thanks to Velden and LadyHLG for their suggestions, I have now sorted this using both of their suggestions. All my client needs to do now is add their unique Google Analytics site reference (i.e. UA-XXXXXXX-1): Here's what we have now that works.

Here's the GCB with the smarty call listed first you'll call it with:

Code: Select all

{global_content name='google_analytics_repeat'}
Here's the GCB content

Code: Select all

{$cgsimple->get_page_content('home','Google_Analytics','foo')}
{if !empty($foo)}
{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}{$foo}', '{$smarty.server.HTTP_HOST}{literal}');
  ga('send', 'pageview');
</__script>{/literal}
{/if}
This goes in the head section of your homepage template:

Code: Select all

{content block='Google_Analytics' oneline='true' label='Google Analytics code only - i.e. UA-XXXXXXX-X' assign='Google_Analytics' wysiwyg='false'}
{global_content name='google_analytics_repeat'}
Then on each of the sites other templates, to replicate the script across every page just add:

Code: Select all

{global_content name='google_analytics_repeat'}
This will add/replicate the GA stuff sitewide without your client having to add the GA code on every page themselves.

So you can now let your clients implement their own Google Analytics from the homepage (Content --> Pages --> Home) in their admin section by just adding their UA-XXXXXXX-X reference and it will be distributed sitewide, keeping it simple for them with no script editing, and no need for a GA plugin. Again, thanks to everyone for their input and help.
Going up, woop, woop.
Post Reply

Return to “Modules/Add-Ons”