Page 1 of 2
[SOLVED] Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 3:46 pm
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.
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 4:01 pm
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}
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 4:13 pm
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?
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 4:14 pm
by calguy1000
Remove the if statement from the homepage template, and just call the same GCB.
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 4:22 pm
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.
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 8:53 pm
by velden
Wrap the {$Google_Analytics} in homepage template in {literal} tags.
Re: Google Analytics - New Version Causing Problems
Posted: Wed Apr 09, 2014 8:56 pm
by Rolf
Add a space before and after all { and } brackets in the GA code, then no need for literal tags...
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 7:07 am
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.
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 8:56 am
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!
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 9:03 am
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.
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 9:06 am
by Jo Morg
Of course... anything between the literal tags won't be parsed by Smarty so {literal}{$Google_Analytics}{/literal} won't work...
I don't think there is an easy way to do it. Rolf's suggestion seems the best for the moment.
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 9:10 am
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...
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.
Re: Google Analytics - New Version Causing Problems
Posted: Thu Apr 10, 2014 9:19 am
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!
Re: Google Analytics - New Version Causing Problems
Posted: Fri Apr 11, 2014 6:50 pm
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
Re: Google Analytics - New Version Causing Problems
Posted: Mon Apr 14, 2014 9:15 am
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.