How to Add Google Analytics Code in Joomla 6 Cassiopeia Template
In Joomla 6, the Cassiopeia template manages your site’s HTML structure. If you want Google Analytics tracking, you need to place the GA snippet inside the <head> section of the default Cassiopeia index.php. Many admins mistakenly try to edit their extended template, but that file doesn’t contain <head>. Here’s how to do it correctly.
<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘G-XXXXXXXXXX’);
</script>
📌 Place your Google Analytics tracking code just before the closing </head> tag in the default Cassiopeia index.php file.
Always take a backup of the original template files before editing. If Joomla or Cassiopeia is updated, your changes may be overwritten — having a backup ensures you can quickly re‑apply the Google Analytics code.
Tips for Admins
- After Joomla or template updates, re‑check that the GA code is still present.
- For update‑proof setups, use
$this->addScriptDeclaration()in your extended template or install a Google Analytics plugin for Joomla. - Test tracking with Google Tag Assistant or GA4 realtime reports.
Click to read in Sinhala / සිංහලෙන් කියවීමට මෙතැන ක්ලික් කරන්න
Joomla 6 Cassiopeia Template එකට Google Analytics Code එක ඇතුළත් කරන ආකාරය
Joomla 6 Cassiopeia template එකේ <head> section එක තුළ Google Analytics tracking code එක දාන්න ඕන. බොහෝ adminලා extended template එක edit කරනවා, නමුත් ඒකේ <head> නැති නිසා tracking වැඩ නොකරයි. නිවැරදිව කරන්න ඕනේ default Cassiopeia index.php edit කිරීමයි.
<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘G-XXXXXXXXXX’);
</script>
📌 ඔබගේ මේ ආකාරයේ code block එක </head> tag එකට පෙර Cassiopeia index.php file එකේ දාන්න.
Edit කරන කලින් original template files backup එකක් ගන්න. Joomla හෝ Cassiopeia update වෙද්දී ඔබේ changes මකා යා හැකියි. Backup එකක් තියෙනවා නම් Google Analytics code එක නැවත ඉක්මනින් දාගන්න පුළුවන්.
Adminලාට උපදෙස්
- Joomla හෝ template update එකක් පසු, GA code එක තවමත් තියෙනවාද කියලා re-check කරන්න.
- Update safe setup එකක් ඕනේ නම්
$this->addScriptDeclaration()extended template එකේ භාවිතා කරන්න, හෝ Google Analytics plugin එකක් install කරන්න. - Tracking test කරන්න Google Tag Assistant හෝ GA4 realtime reports වලින්.
Takeaway: Cassiopeia default template එකේ <head> section එකේ GA code එක දාද්දී Joomla 6 site එකේ traffic tracking නිවැරදිව වැඩ කරයි. Kapothi readersට මේක safe සහ practical solution එකක්.