Skip to content

Commit

Permalink
Render the tag in head
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBelgium committed Jun 21, 2024
1 parent 238317a commit 7f2492c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
*/

use Flarum\Extend;
use Michaelbelgium\FlarumUmami\Listeners;

return [
(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/less/admin.less'),

(new Extend\Frontend('forum'))
->content(Listeners\UmamiTagRenderer::class),

new Extend\Locales(__DIR__.'/locale'),
];
28 changes: 28 additions & 0 deletions src/Listeners/UmamiTagRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Michaelbelgium\FlarumUmami\Listeners;

use Flarum\Frontend\Document;
use Flarum\Settings\SettingsRepositoryInterface;
use Psr\Http\Message\ServerRequestInterface;

class UmamiTagRenderer
{
protected SettingsRepositoryInterface $settings;

public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}

public function __invoke(Document $document, ServerRequestInterface $request)
{
$domain = $this->settings->get('michaelbelgium-umami.domain');
$site_id = $this->settings->get('michaelbelgium-umami.site_id');

if (str_ends_with($domain, '/'))
$domain = substr($domain, 0, -1);

$document->head[] = "<script defer data-website-id=\"$site_id\" src=\"$domain/script.js\"></script>";
}
}

0 comments on commit 7f2492c

Please sign in to comment.