From f830ee0a16ca7240b3bb7d142e5d7db4c61bce31 Mon Sep 17 00:00:00 2001 From: Nguyen Ngoc Cuong Date: Wed, 29 Mar 2023 18:13:43 +0700 Subject: [PATCH] Fix for Google Analytics 4 Fix new script for Google Analytics 4 and add `user_id` field --- init.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/init.php b/init.php index f302dbc..970dd13 100644 --- a/init.php +++ b/init.php @@ -35,19 +35,23 @@ function ganalytics_add_code() { + $webPropertyId = trim(OW::getConfig()->getValue('ganalytics', 'web_property_id')); + $userId = OW::getUser()->getId(); + $sUid = ''; + if ($userId > 0) + $sUid = '\'user_id\':\''. $userId .'\','; + + $code = ' + + - '; + gtag(\'config\', \''. $webPropertyId . '\', {' . $sUid. '}); + '; - OW::getDocument()->appendBody($code); + OW::getDocument()->addCustomHeadInfo($code); } OW::getEventManager()->bind(OW_EventManager::ON_FINALIZE, 'ganalytics_add_code'); }