iflrandevu/application/helpers/google_analytics_helper.php

60 lines
2.5 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.1.0
* ---------------------------------------------------------------------------- */
/**
* Print Google Analytics script.
*
* This helper function should be used in view files in order to output the Google Analytics script. It will check
* whether the code is set in the database and print it, otherwise nothing will be outputted. This eliminates the need
* for extra checking before outputting.
*/
function google_analytics_script()
{
$google_analytics_code = setting('google_analytics_code');
if ($google_analytics_code !== '')
{
// If the Google Analytics code starts with UA then it is a Universal Analytics Property and the script stays
// the legacy one.
if (substr($google_analytics_code, 0, 2) === 'UA')
{
echo '
<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", "' . $google_analytics_code . '", "auto");
ga("send", "pageview");
</script>
';
}
// If the Google Analytics code starts with a G then it is a Google Analytics 4-Property and the script
// to inject it looks different.
if (substr($google_analytics_code, 0, 2) === 'G-')
{
echo '
<script async src="https://www.googletagmanager.com/gtag/js?id=' . $google_analytics_code . '"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "' . $google_analytics_code . '");
</script>
';
}
}
}