<commit_before><?php

namespace Flagrow\Analytics\Listeners;

use Flarum\Core\Guest;
use Flarum\Core\User;
use Flarum\Event\ConfigureClientView;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Settings\SettingsRepositoryInterface;

class AddTrackingJs
{
    /**
     * @var SettingsRepositoryInterface
     */
    protected $settings;

    /**
     * AddTrackingJs constructor.
     *
     * @param SettingsRepositoryInterface $settings
     */
    public function __construct(SettingsRepositoryInterface $settings)
    {
        $this->settings = $settings;
    }

    /**
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events)
    {
        $events->listen(ConfigureClientView::class, [$this, 'addAssets']);
    }

    /**
     * @param ConfigureClientView $event
     */
    public function addAssets(ConfigureClientView $event)
    {
        if ($event->isForum()) {
            // Add google analytics if tracking UA has been configured.
            if ($this->settings->get('flagrow.analytics.statusGoogle') && $this->settings->get('flagrow.analytics.googleTrackingCode')) {
                $rawJs = file_get_contents(realpath(__DIR__ . '/../../assets/js/google-analytics.js'));
                $js = str_replace("%%TRACKING_CODE%%", $this->settings->get('flagrow.analytics.googleTrackingCode'), $rawJs);
                $event->view->addHeadString($js);
            }

            // get the validation data
            $settings = [
                'statusPiwik' => $this->settings->get('flagrow.analytics.statusPiwik'),
                'piwikUrl' => $this->settings->get('flagrow.analytics.piwikUrl'),
                'piwikSiteId' => $this->settings->get('flagrow.analytics.piwikSiteId'),
            ];
            // Add piwik specific tracking code if configured in admin.
            if ($settings['statusPiwik'] && $settings['piwikUrl'] && $settings['piwikSiteId']) {
                // get all the data
                $settings += [
                    'piwikHideAliasUrl' => $this->settings->get('flagrow.analytics.piwikHideAliasUrl'),
                    'piwikAliasUrl' => $this->settings->get('flagrow.analytics.piwikAliasUrl'),
                    'piwikTrackSubdomain' => $this->settings->get('flagrow.analytics.piwikTrackSubdomain'),
                    'piwikPrependDomain' => $this->settings->get('flagrow.analytics.piwikPrependDomain'),
                    'piwikTrackAccounts' => $this->settings->get('flagrow.analytics.piwikTrackAccounts'),
                ];

                $rawJs = file_get_contents(realpath(__DIR__ . '/../../assets/js/piwik-analytics.js'));

                $options = [];

                $options[] = "_paq.push(['setSiteId', " . $settings['piwikSiteId'] . "]);";

                if ($settings['piwikTrackSubdomain']) {
                    $options[] = "_paq.push(['setCookieDomain', '*." . $_SERVER['HTTP_HOST'] . "']);";
                }

                if ($settings['piwikPrependDomain']) {
                    $options[] = "_paq.push(['setDocumentTitle', document.domain + '/' + document.title]);";
                }

                if ($settings['piwikHideAliasUrl'] && $settings['piwikAliasUrl']) {
                    $options[] = "_paq.push(['setDomains', ['*." . $settings['piwikAliasUrl'] . "']]);";
                }

                if (in_array($settings['piwikTrackAccounts'], ['username', 'email'])) {
                    $user = $event->request->getAttribute('actor');

                    if ($user instanceof User && !($user instanceof Guest)) {
                        $userId = $user->{$settings['piwikTrackAccounts']};

                        $options[] = "_paq.push(['setUserId', " . json_encode($userId) . "]);";<commit_msg>Remove unnecessary check for type User<commit_after><?php

namespace Flagrow\Analytics\Listeners;

use Flarum\Core\Guest;
use Flarum\Core\User;
use Flarum\Event\ConfigureClientView;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Settings\SettingsRepositoryInterface;

class AddTrackingJs
{
    /**
     * @var SettingsRepositoryInterface
     */
    protected $settings;

    /**
     * AddTrackingJs constructor.
     *
     * @param SettingsRepositoryInterface $settings
     */
    public function __construct(SettingsRepositoryInterface $settings)
    {
        $this->settings = $settings;
    }

    /**
     * @param Dispatcher $events
     */
    public function subscribe(Dispatcher $events)
    {
        $events->listen(ConfigureClientView::class, [$this, 'addAssets']);
    }

    /**
     * @param ConfigureClientView $event
     */
    public function addAssets(ConfigureClientView $event)
    {
        if ($event->isForum()) {
            // Add google analytics if tracking UA has been configured.
            if ($this->settings->get('flagrow.analytics.statusGoogle') && $this->settings->get('flagrow.analytics.googleTrackingCode')) {
                $rawJs = file_get_contents(realpath(__DIR__ . '/../../assets/js/google-analytics.js'));
                $js = str_replace("%%TRACKING_CODE%%", $this->settings->get('flagrow.analytics.googleTrackingCode'), $rawJs);
                $event->view->addHeadString($js);
            }

            // get the validation data
            $settings = [
                'statusPiwik' => $this->settings->get('flagrow.analytics.statusPiwik'),
                'piwikUrl' => $this->settings->get('flagrow.analytics.piwikUrl'),
                'piwikSiteId' => $this->settings->get('flagrow.analytics.piwikSiteId'),
            ];
            // Add piwik specific tracking code if configured in admin.
            if ($settings['statusPiwik'] && $settings['piwikUrl'] && $settings['piwikSiteId']) {
                // get all the data
                $settings += [
                    'piwikHideAliasUrl' => $this->settings->get('flagrow.analytics.piwikHideAliasUrl'),
                    'piwikAliasUrl' => $this->settings->get('flagrow.analytics.piwikAliasUrl'),
                    'piwikTrackSubdomain' => $this->settings->get('flagrow.analytics.piwikTrackSubdomain'),
                    'piwikPrependDomain' => $this->settings->get('flagrow.analytics.piwikPrependDomain'),
                    'piwikTrackAccounts' => $this->settings->get('flagrow.analytics.piwikTrackAccounts'),
                ];

                $rawJs = file_get_contents(realpath(__DIR__ . '/../../assets/js/piwik-analytics.js'));

                $options = [];

                $options[] = "_paq.push(['setSiteId', " . $settings['piwikSiteId'] . "]);";

                if ($settings['piwikTrackSubdomain']) {
                    $options[] = "_paq.push(['setCookieDomain', '*." . $_SERVER['HTTP_HOST'] . "']);";
                }

                if ($settings['piwikPrependDomain']) {
                    $options[] = "_paq.push(['setDocumentTitle', document.domain + '/' + document.title]);";
                }

                if ($settings['piwikHideAliasUrl'] && $settings['piwikAliasUrl']) {
                    $options[] = "_paq.push(['setDomains', ['*." . $settings['piwikAliasUrl'] . "']]);";
                }

                if (in_array($settings['piwikTrackAccounts'], ['username', 'email'])) {
                    $user = $event->request->getAttribute('actor');

                    if (!($user instanceof Guest)) {
                        $userId = $user->{$settings['piwikTrackAccounts']};

                        $options[] = "_paq.push(['setUserId', " . json_encode($userId) . "]);";
{'fraction_non_alphanumeric': 0.16013333333333332, 'fraction_numerical': 0.0, 'mean_word_length': 2.2471861471861474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '498554', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2026, 'n_words': 302}