This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/google_analytics/main.php
Shish f84bcaec01 microhtml for everything in <head>
I wanted to ensure that all pages (even the downtime page, terms page, home page, etc) had the appropriate `data-` attributes on `<body>` (because those are required for certain javascript, eg autocomplete, to work). One thing led to another and now everything in `head` is microhtml'ed
2024-06-21 18:52:05 +01:00

38 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Shimmie2;
use function MicroHTML\SCRIPT;
class GoogleAnalytics extends Extension
{
# Add analytics to config
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Google Analytics");
$sb->add_text_option("google_analytics_id", "Analytics ID: ");
$sb->add_label("<br>(eg. UA-xxxxxxxx-x)");
}
# Load Analytics tracking code on page request
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $page;
$google_analytics_id = $config->get_string('google_analytics_id', '');
if (stristr($google_analytics_id, "UA-")) {
$page->add_html_header(SCRIPT(["type" => 'text/javascript'], "
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '$google_analytics_id']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
"));
}
}
}