Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move analytics code to preload #1429

Merged
merged 6 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions htdocs/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,7 @@
*/
$icmsTheme = $xoTheme = &$xoopsThemeFactory->createInstance(array('contentTemplate' => @$xoopsOption['template_main']));
$xoopsTpl = $icmsTpl = &$xoTheme->template;
// no longer needed because of ticket #751
// if ($icmsConfigMetaFooter['use_google_analytics'] === TRUE
// && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') {
// /* Legacy GA urchin code */
// //$xoTheme->addScript('http://www.google-analytics.com/urchin.js',array('type' => 'text/javascript'),'_uacct = "UA-' . $icmsConfigMetaFooter['google_analytics'] . '";urchinTracker();');
// $scheme = parse_url(ICMS_URL, PHP_URL_SCHEME);
// if ($scheme == 'http') {
// /* New GA code, http protocol */
// $xoTheme->addScript('http://www.google-analytics.com/ga.js', array('type' => 'text/javascript'),'');
// } elseif ($scheme == 'https') {
// /* New GA code, https protocol */
// $xoTheme->addScript('https://ssl.google-analytics.com/ga.js', array('type' => 'text/javascript'),'');
// }
// }

if (isset($icmsConfigMetaFooter['google_meta']) && $icmsConfigMetaFooter['google_meta'] != '') {
$xoTheme->addMeta('meta', 'verify-v1', $icmsConfigMetaFooter['google_meta']);
$xoTheme->addMeta('meta', 'google-site-verification', $icmsConfigMetaFooter['google_meta']);
Expand Down
13 changes: 0 additions & 13 deletions htdocs/libraries/icms/view/theme/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,6 @@ public function xoInit($options = array()) {
$this->addMeta('meta', substr($name, 5), $value);
} elseif (substr($name, 0, 6) == 'footer') {
$values = $value;
if ($icmsConfigMetaFooter['use_google_analytics'] == TRUE && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') {

$values = $value . "<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-" . $icmsConfigMetaFooter['google_analytics'] . "', 'auto');
ga('send', 'pageview');

</script>";
}
$this->template->assign("xoops_$name", $values);
$this->template->assign("icms_$name", $values);
} else {
Expand Down
42 changes: 42 additions & 0 deletions htdocs/plugins/preloads/googleanalytics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* googleanalytics ga4.php
* Created by david on 13/02/2022 22:41
* Modified by skenow on 11 June 2023
*/

/**
* preload class to include the Google Analytics V4 tag in the Head of the webpage, after replacing the smarty variable with the GA4 property code
* This can certainly be improved by checking whether we can, based on the preferences by our users (GDPR and cookies)
*/
class IcmsPreloadGa4 extends IcmsPreloadItem {

function eventBeforeFooter() {
global $xoopsTpl;
global $icmsTheme;
global $icmsConfigMetaFooter;
global $xoTheme;

try {

if ($icmsConfigMetaFooter['use_google_analytics'] == TRUE && isset($icmsConfigMetaFooter['google_analytics']) && $icmsConfigMetaFooter['google_analytics'] != '') {
if (substr($icmsConfigMetaFooter['google_analytics'], 0, 2 ) === "G-") {
$tagvalue = $icmsConfigMetaFooter['google_analytics'];
} else {
$tagvalue = 'UA-' . $icmsConfigMetaFooter['google_analytics'];
}
fiammybe marked this conversation as resolved.
Show resolved Hide resolved
$xoTheme->addScript('https://www.googletagmanager.com/gtag/js?id=' . $tagvalue, ['async'=>'async'], '', 'module', 2000);
$xoTheme->addScript('', '', 'window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\'js\', new Date());

gtag(\'config\', \'' . $tagvalue . '\');', 'module', '2001');
} else {
echo 'error getting icmsConfigMetaFooter';
}
}
catch (Exception $e) {
echo $e->getMessage();
}
}
}