Skip to content

Instantly share code, notes, and snippets.

@alexandreelise
Created April 7, 2024 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreelise/a0b5649802e349258788a8d67419cbd8 to your computer and use it in GitHub Desktop.
Save alexandreelise/a0b5649802e349258788a8d67419cbd8 to your computer and use it in GitHub Desktop.

One-liner to use plausible.io Web Analytics on your Joomla Website

Joomla\CMS\Factory::getApplication()->getDocument()->getWebAssetManager()->registerAndUseScript('plausible-io', 'https://plausible.io/js/script.js', [], ['defer' => true, 'data-domain' => \Joomla\CMS\Uri\Uri::getInstance()->getHost()], []);

WHY

Scratched my own itch. Thought it might be useful to you too.

WHAT

Joomla\CMS\Factory::getApplication()->getDocument()->getWebAssetManager()->registerAndUseScript('plausible-io', 'https://plausible.io/js/script.js', [], ['defer' => true, 'data-domain' => \Joomla\CMS\Uri\Uri::getInstance()->getHost()], []);

HOW

This file should be placed in your/joomla/site/root/templates/your_template_name/html/mod_custom/onelinerplausibleio.php It is a mod_custom layout override. The mod_custom module with this layout override should be in position debug or equivalent.

<?php
/**
* This file should be place in your/joomla/site/root/templates/your_template_name/html/mod_custom/
* It is a mod_custom layout override. The mod_custom module with this layout override should be in position debug or equivalent.
*
* @copyright (c) 2009 - present. Mr Alexandre J-S William ELISÉ. All rights reserved.
* @license GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later). See LICENSE.txt file
*/
defined('_JEXEC') or die;
// Example snippet that should be placed in head in HTML <script defer data-domain="example.org" src="https://plausible.io/js/script.js"></script>
// One-liner to add plausible.io Web Analytics script with dynamically infered domain name using Joomla CMS API
\Joomla\CMS\Factory::getApplication()->getDocument()->getWebAssetManager()->registerAndUseScript('plausible-io', 'https://plausible.io/js/script.js', [], ['defer' => true, 'data-domain' => \Joomla\CMS\Uri\Uri::getInstance()->getHost()], []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment