[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.
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: [SOLVED] Google Analytics - New Version Causing Problems

Post by velden »

One little note:

Maybe it's wise to use a trim function on the foo variable. In case an editor accidently pastes a (invisible) space inside the box. That would possibly break the GA function.

I did not test it but I think {trim($foo)} and/or {$foo|trim} should work.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: [SOLVED] Google Analytics - New Version Causing Problems

Post by govicinity »

@Velden, thanks, great suggestion, just tried this on one of the sites we run

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|trim}', '{$smarty.server.HTTP_HOST}{literal}');
  ga('send', 'pageview');
</__script>{/literal}
{/if}
Added an extra space at the end of the GA code to test it, and it stripped it out nicely.
Going up, woop, woop.
govicinity
Forum Members
Forum Members
Posts: 125
Joined: Tue Nov 22, 2011 2:22 pm

Re: [SOLVED] Google Analytics - New Version Causing Problems

Post by govicinity »

PS, anyone using this, please make sure you remove the underscores from the <__script__> </__script> tags in the code I have provided, the forum adds this automatically to stop pasted code interfering with the forum site.
Going up, woop, woop.
Post Reply

Return to “Modules/Add-Ons”