Skip to content

Commit

Permalink
Fix errors when only Google tracking code is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Morland committed Jun 21, 2020
1 parent 57320e6 commit c1ab797
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
19 changes: 19 additions & 0 deletions resources/js/google-optimize.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- Google Optimise -->
<!-- Page-hiding snippet (recommended) -->
<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',2000,
{'%%OPT_TRACKING_CODE%%':true});</script>
<!-- Google Analytics with Optimize plugin -->
<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', '%%TRACKING_CODE%%', 'auto');
ga('require', '%%OPT_TRACKING_CODE%%');
</script>
<!-- End Google Optimise -->

18 changes: 0 additions & 18 deletions resources/js/google-tag-manager.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
<!-- Google Optimise -->
<!-- Page-hiding snippet (recommended) -->
<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};
(a[n]=a[n]||[]).hide=h;setTimeout(function(){i();h.end=null},c);h.timeout=c;
})(window,document.documentElement,'async-hide','dataLayer',2000,
{'%%OPT_TRACKING_CODE%%':true});</script>
<!-- Modified Analytics tracking code with Optimize plugin -->
<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', '%%TRACKING_CODE%%', 'auto');
ga('require', '%%OPT_TRACKING_CODE%%');
</script>
<!-- End Google Optimise -->
<!-- Google Tag Manager -->
<script>
function gtagpush(virtualpath){dataLayer.push(virtualpath);}
Expand Down
28 changes: 18 additions & 10 deletions src/Listeners/AddTrackingJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,39 @@ public function __invoke(Document $document, ServerRequestInterface $request)

private function analytics(Document &$document)
{
if($statusGoogle = $this->settings->get('fof-analytics.statusGoogle')) {
$js = file_get_contents(realpath(__DIR__ . '/../../resources/js/google-tag-manager.html'));

// Add google analytics if tracking UA has been configured.
if ($googleTrackingCode = $this->settings->get('fof-analytics.googleTrackingCode')) {
$statusGoogle = (bool) $this->settings->get('fof-analytics.statusGoogle');
$googleTrackingCode = $this->settings->get('fof-analytics.googleTrackingCode');
$googleGTMCode = $this->settings->get('fof-analytics.googleGTMCode');
$optTrackingCode = $this->settings->get('fof-analytics.optTrackingCode');

if($statusGoogle) {
// Add google analytics if tracking UA only has been configured.
if ($googleTrackingCode && !$googleGTMCode && !$optTrackingCode) {
$js = file_get_contents(realpath(__DIR__ . '/../../resources/js/google-analytics.html'));
$js = str_replace("%%TRACKING_CODE%%", $googleTrackingCode, $js);

$document->payload['googleTrackingCode'] = $googleTrackingCode;
$document->head[] = $js;
}

// Add google tag manager if tracking GTM has been configured.
if ($googleGTMCode = $this->settings->get('fof-analytics.googleGTMCode')) {
if ($googleGTMCode) {
$js = file_get_contents(realpath(__DIR__ . '/../../resources/js/google-tag-manager.html'));
$js = str_replace("%%GTM_TRACKING_CODE%%", $googleGTMCode, $js);
$js = str_replace("%%TRACKING_CODE%%", $googleTrackingCode, $js);

$document->payload['googleGTMCode'] = $googleGTMCode;
$document->head[] = $js;
}

if ($optTrackingCode = $this->settings->get('fof-analytics.optTrackingCode')) {
// Add google analytics with google optimize plugin if configured
if ($googleTrackingCode && $optTrackingCode) {
$js = file_get_contents(realpath(__DIR__ . '/../../resources/js/google-optimize.html'));
$js = str_replace("%%OPT_TRACKING_CODE%%", $optTrackingCode, $js);
$js = str_replace("%%TRACKING_CODE%%", $googleTrackingCode, $js);

$document->payload['optTrackingCode'] = $optTrackingCode;
$document->head[] = $js;
}

$document->head[] = $js;
}
}

Expand Down

0 comments on commit c1ab797

Please sign in to comment.