overture-stack / website

Overture.bio website, based on Gatsby static site generator
GNU Affero General Public License v3.0
1 stars 2 forks source link

Matomo GitHub Plugin #328

Closed MitchellShiell closed 9 months ago

MitchellShiell commented 11 months ago

GitHub Metrics Matomo Plugin

Our analytics for the Overtures website is within Matomo. This is convenient as it is all archived within the platform and can be set up to send out emailed reports at regular intervals. Integrating our GitHub Repo Metrics into Matomo makes the dashboard/platform an all-in-one resource.

I gave it a go but only go as far as rendering the JSON response from Github on the Matomo dashboard. I've outlined general steps below:

| Repository | Timestamp | Unique Clones | Clones |

<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\GitHubInsights;

use Piwik\Settings\Setting;
use Piwik\Settings\FieldConfig;
use Piwik\Validators\NotEmpty;

/**
 * Defines Settings for GitHubInsights.
 *
 * Usage like this:
 * $settings = new SystemSettings();
 * $settings->metric->getValue();
 * $settings->description->getValue();
 */
class SystemSettings extends \Piwik\Settings\Plugin\SystemSettings
{

    /** @var Setting */
    public $githubProfile;  // GitHub profile setting declaration

    /** @var Setting */
    public $trackedRepos;  // Tracked repositories setting declaration

    /** @var Setting */
    public $apiKey;  // API key setting declaration

    protected function init()
    {
        // GitHub Profile initialization
        $this->githubProfile = $this->createGitHubProfileSetting();

        // GitHub Repos initialization
        $this->trackedRepos = $this->createRepoSetting();

        // API key setting initialization
        $this->apiKey = $this->createAPISetting();
    }

    private function createGitHubProfileSetting()
    {
        return $this->makeSetting('githubProfile', $default = '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
            $field->title = 'GitHub Profile';
            $field->uiControl = FieldConfig::UI_CONTROL_TEXT;
            $field->description = 'Enter the GitHub profile you want to track';
            $field->validators[] = new NotEmpty();
        });
    }

    private function createRepoSetting()
    {
        return $this->makeSetting('trackedRepos', $default = '', FieldConfig::TYPE_ARRAY, function (FieldConfig $field) {
            $field->title = 'Tracked Repositories';
            $field->uiControl = FieldConfig::UI_CONTROL_TEXTAREA;
            $field->description = 'Enter the names of the GitHub repositories you want to track, one per line.';
        });
    }

    private function createAPISetting()
    {
        return $this->makeSetting('apiKey', $default = null, FieldConfig::TYPE_STRING, function (FieldConfig $field) {
            $field->title = 'API Key';
            $field->uiControl = FieldConfig::UI_CONTROL_TEXT;
            $field->description = 'Enter your GitHub API key here [ADD DETAILED DESCRIPTION]';
            $field->validators[] = new NotEmpty();
        });
    }

    public function getTrackedRepositories()
    {
        $value = $this->trackedRepos->getValue();
        if (!is_string($value)) {
            return [];  // Return an empty array if the value is not a string
        }
        return explode("\n", str_replace("\r", "", $value));
    }

}