I've searched the forum and it seems that I'm going to have to figure out how to gain access to the header in order to add the GA tag.
Is this correct?
No way to install Google Analytics tag
- Kofod95
- Senior Contributor
- Posts: 748
- Joined: Sat Feb 06, 2021 7:38 pm
- Phoenix Version: 1.0.8.20
- Has thanked: 99 times
- Been thanked: 179 times
Re: No way to install Google Analytics tag
A hook or a header-tag module can add things to the header quite easily.
At least one add-on already exist: app.php/addons/paid_addon/google_analytics_4
//Daniel
At least one add-on already exist: app.php/addons/paid_addon/google_analytics_4
//Daniel
I'm not smart, but sometimes even a blind chicken can find a corn.
Here are a lot of corns: Phoenix user guide
Here are a lot of corns: Phoenix user guide
- Pierre_P
- Contributor
- Posts: 144
- Joined: Fri Mar 12, 2021 5:06 am
- Phoenix Version: v1.1.0.6
- Has thanked: 21 times
- Been thanked: 11 times
Re: No way to install Google Analytics tag
@Kofod95
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.
includes/modules/header_tags/ht_google_analytics.php
If code checks out - thumbs up or more work needed to make it happen
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.
includes/modules/header_tags/ht_google_analytics.php
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.',
],
];
}
}-
Philo2005
- Member
- Posts: 49
- Joined: Fri Mar 26, 2021 7:20 am
- Phoenix Version: v1.0.8.0
- Has thanked: 28 times
- Been thanked: 3 times
Re: No way to install Google Analytics tag
I have received a hook from a programer who is doing GA 4 Tag a while ago.
Attached the source code:
Just replace my gtag Id with yours.
Add this code to includes/hook/shop/siteWide/googleTag.php.
This should do the job.
Attached the source code:
Just replace my gtag Id with yours.
Code: Select all
<?php
class hook_shop_siteWide_googleTag{
function listen_injectSiteStart(){
$tag_script = <<<SCRIPTX
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-1041919398"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-1041919398');
</script>
SCRIPTX;
return $tag_script;
}
}
This should do the job.
-
BrockleyJohn
- Certified Developer
- Posts: 173
- Joined: Mon Mar 01, 2021 5:37 pm
- Phoenix Version:
- : Buy Me A Beverage
- Has thanked: 2 times
- Been thanked: 30 times
- drtom
- Member
- Posts: 49
- Joined: Sat May 24, 2025 3:59 am
- Phoenix Version: v1.1.0.2
- Has thanked: 21 times
- Been thanked: 3 times
Re: No way to install Google Analytics tag
Thank you good people!
I think @Philo2005's suggestion did the trick. Just waiting to see if Google crawls my site and gives me feedback.
Yes, I've finally gone live. Still have a lot of work to do, but at least my hat's in the ring.
I think @Philo2005's suggestion did the trick. Just waiting to see if Google crawls my site and gives me feedback.
Yes, I've finally gone live. Still have a lot of work to do, but at least my hat's in the ring.