Custom Fields in template.php for GA enhanced ecommerce JS

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
CitW
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Apr 12, 2018 8:24 am

Custom Fields in template.php for GA enhanced ecommerce JS

Post by CitW » Thu Apr 12, 2018 8:40 am

Hello

I need to add some enhanced ecommerce tags to standard Google Analytics tracking on few pages. I think the best way to do it would be to use Joomla Custom fields so I created a custom field for testing:

The custom field is:

ID: '1'
Name: 'ec-js'
Automatic display - off
type: text filter: no
Show on: both

value (in one article; for testing): 'ga('require', 'ec');'

The template is Yootheme Infinite (Warp 6 based). I put the custom code in 'template.php', not in article override because I need it on every page including /search, categories etc.

I have gone through Joomla Docs but I'm a beginner...ATM I have:

Top of the page php:

Code: Select all

<?php
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

$jcFields = FieldsHelper::getFields('com_content.article',  $item, True);

foreach($jcFields as $jcField)
{
  $jcFields[$jcField->name] = $jcField;
}
?>
then in the body content:

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','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xxxx', 'auto');
ga('require','displayfeatures');

<?php echo $article->jcFields['ec-js']->value; ?>

ga('send', 'pageview');
</script>
The main GA code is displayed on all pages as it should be.

The problem is that the contents of the custom field are not displayed on the test page containing the field value - the override is clearly not working, something is definitely missing :(

Help with this would be appreciated.

Many thanks.

CitW
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Thu Apr 12, 2018 8:24 am

Re: Custom Fields in template.php for GA enhanced ecommerce JS

Post by CitW » Fri Apr 13, 2018 1:27 pm

I have made some good progress...

I override article default.php with the following PHP:

Code: Select all

$CCFields = array();
   foreach($this->item->jcfields as $field) {
      $CCFields[$field->name] = $field->value;
  } 

$GLOBALS['CCFields'] = $CCFields;
then add to template.php:

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','https://www.google-analytics.com/analytics.js','ga');

				ga('create', 'UA-xxxx', 'auto');
				ga('require','displayfeatures');
				<?php echo $GLOBALS['CCFields']['ec-js'] ?>
				ga('send', 'pageview');
		</script>

The result is GA JS with enhanced ecommerce tags nicely injected when needed - only with caching disabled.

If I enable Joomla cache (conservative) the 'ec-js' field is no longer injected meaning the values of $GLOBALS['CCFields'] are not cached and are only available on 1st page load. Any successive page load e.g. 2nd or 3rd results in nothing injected as contents of the array are not cached.

How do I make contents of $GLOBALS['CCFields'] cached?

Thanks.


Locked

Return to “Joomla! 3.x Coding”