That module is long time deprecated for analytics, i had to remake it to work with the latest analytics - busy testing on the newest phoenix version.
Code: Select all
<?php
/*
$Id$
CE Phoenix, E-Commerce made Easy
https://phoenixcart.org
Copyright (c) 2025 Phoenix Cart
Released under the GNU General Public License
*/
class ht_google_analytics extends abstract_executable_module {
const CONFIG_KEY_BASE = 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_';
public function __construct() {
parent::__construct(__FILE__);
if (static::get_constant('MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_JS_PLACEMENT') !== 'Header') {
$this->group = 'footer_scripts';
}
}
public function execute() {
global $currencies;
if (Text::is_empty(MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID)) {
return;
}
$ga_id = Text::output(MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID);
$header = <<<EOJS
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={$ga_id}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{$ga_id}');
</script>
EOJS;
if (isset($_SESSION['customer_id']) && (MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_EC_TRACKING === 'True')
&& (basename(Request::get_page()) === 'checkout_success.php'))
{
// Raiwa discount code added - discount_codes
$order_query = $GLOBALS['db']->query(sprintf(<<<'EOSQL'
SELECT orders_id, discount_codes FROM orders WHERE customers_id = %d ORDER BY date_purchased DESC LIMIT 1
EOSQL
, (int)$_SESSION['customer_id']));
if (mysqli_num_rows($order_query) == 1) {
$order = $order_query->fetch_assoc();
$totals_query = $GLOBALS['db']->fetch_all("SELECT value, class, title FROM orders_total WHERE orders_id = " . (int)$order['orders_id']);
$totals = array_column($totals_query, 'value', 'class');
$titles = array_column($totals_query, 'title', 'class');
$transaction = [
'transaction_id' => (int)$order['orders_id'],
'affiliation' => Text::output(STORE_NAME),
'value' => isset($totals['ot_total']) ? $currencies->format_raw($totals['ot_total'], DEFAULT_CURRENCY) : 0,
'tax' => isset($totals['ot_tax']) ? $currencies->format_raw($totals['ot_tax'], DEFAULT_CURRENCY) : 0,
'shipping' => isset($totals['ot_shipping']) ? $currencies->format_raw($totals['ot_shipping'], DEFAULT_CURRENCY) : 0,
'currency' => DEFAULT_CURRENCY,
'items' => []
];
// Check for discount_codes in orders table first
if (!empty($order['discount_codes'])) {
$transaction['coupon'] = Text::output($order['discount_codes']);
} elseif (isset($titles['ot_discount']) || isset($titles['ot_payment'])) {
// Fallback to generic coupon identifiers
$coupon_parts = [];
if (isset($titles['ot_discount'])) {
$coupon_parts[] = 'DISCOUNT_CODE';
}
// payment type discount
if (isset($titles['ot_payment'])) {
$coupon_parts[] = 'PAYMENT_DISCOUNT';
}
$transaction['coupon'] = Text::output(implode(',', $coupon_parts));
}
$order_products_query = $GLOBALS['db']->query(sprintf(<<<'EOSQL'
SELECT op.products_id, pd.products_name, op.final_price, op.products_quantity, l.languages_id
FROM orders_products op
INNER JOIN products_description pd ON op.products_id = pd.products_id
INNER JOIN languages l ON l.languages_id = pd.language_id
WHERE op.orders_id = %d AND l.code = '%s'
EOSQL
, (int)$order['orders_id'], $GLOBALS['db']->escape(DEFAULT_LANGUAGE)));
while ($order_products = $order_products_query->fetch_assoc()) {
$category_query = $GLOBALS['db']->query(sprintf(<<<'EOSQL'
SELECT cd.categories_name
FROM categories_description cd INNER JOIN products_to_categories p2c ON p2c.categories_id = cd.categories_id
WHERE p2c.products_id = %d AND cd.language_id = %d
LIMIT 1
EOSQL
, (int)$order_products['products_id'], (int)$order_products['languages_id']));
$category = $category_query->fetch_assoc();
$transaction['items'][] = [
'item_id' => (int)$order_products['products_id'],
'item_name' => Text::output($order_products['products_name']),
'item_category' => Text::output($category['categories_name'] ?? 'Uncategorized'),
'price' => $currencies->format_raw($order_products['final_price']),
'quantity' => (int)$order_products['products_quantity']
];
}
$header .= '<script>' . "\n";
$header .= 'gtag("event", "purchase", ' . json_encode($transaction, JSON_UNESCAPED_SLASHES) . ');' . "\n";
$header .= '</script>' . "\n";
}
}
$GLOBALS['Template']->add_block($header, $this->group);
}
protected function get_parameters() {
return [
'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS' => [
'title' => 'Enable Google Analytics Module',
'value' => 'True',
'desc' => 'Do you want to add Google Analytics to your shop?',
'set_func' => "Config::select_one(['True', 'False'], ",
],
'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID' => [
'title' => 'Google Analytics ID',
'value' => ' ',
'desc' => 'The Google Analytics property ID (G-XXXXXXXXXX) to track.',
],
'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_EC_TRACKING' => [
'title' => 'E-Commerce Tracking',
'value' => 'True',
'desc' => 'Do you want to enable enhanced e-commerce tracking? (E-Commerce tracking must also be enabled in your Google Analytics property settings)',
'set_func' => "Config::select_one(['True', 'False'], ",
],
'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_JS_PLACEMENT' => [
'title' => 'Javascript Placement',
'value' => 'Header',
'desc' => 'Should the Google Analytics javascript be loaded in the header or footer?',
'set_func' => "Config::select_one(['Header', 'Footer'], ",
],
'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_SORT_ORDER' => [
'title' => 'Sort Order',
'value' => '0',
'desc' => 'Sort order of display. Lowest is displayed first.',
],
];
}
}