Skip to content

Commit

Permalink
feat: matomo and podtrack support
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Jun 9, 2023
1 parent 51a99a7 commit dc8f44b
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 26 deletions.
8 changes: 5 additions & 3 deletions app/PodcasterStats.interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
interface PodcasterStatsInterfaceBase
{
public function stopIfIsBot(string $userAgentData): bool;
public function trackFeed($feed): void;
public function trackEpisode($feed, $episode, $userAgent): void;
public function trackEpisodeMatomo(): void;
public function trackPodTrac(): void;
public function trackEpisodeMatomo($feed, $episode): void;
public function trackFeedMatomo($feed): void;
public function trackPodTrac($feed, $episode): void;
public function getUserAgent(string $userAgentData): array;
public function getFeedQueryData($feed): array;
public function getEpisodeQueryData($feed, $episode, $trackingDate): array;
Expand All @@ -17,7 +19,7 @@ public function formatTrackingDate(int $timestamp): string;

interface PodcasterStatsInterface extends PodcasterStatsInterfaceBase
{
public function trackFeed($feed): void;
public function upsertFeed($feed): void;
public function upsertEpisode($feed, $episode, $trackingDate): void;
public function upsertUserAgents($feed, array $userAgentData, int $trackingDate): void;
public function getDownloadsGraphData($podcast, $year, $month): object | bool;
Expand Down
63 changes: 49 additions & 14 deletions app/PodcasterStats.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,72 @@ public function trackEpisode($feed, $episode, $userAgent): void
$this->upsertUserAgents($feed, $userAgentData, $trackingDate);
}

$this->trackEpisodeMatomo();
$this->trackPodTrac();
$this->trackEpisodeMatomo($feed, $episode);
$this->trackPodTrac($feed, $episode);
}

public function trackFeed($feed): void
{
if (!isset($feed)) {
return;
}

if (option('mauricerenck.podcaster.statsInternal') === true) {
$trackingDate = time();
$this->upsertFeed($feed);
}

$this->trackFeedMatomo($feed);
}

public function upsertEpisode($feed, $episode, $trackingDate)
{
return;
}


public function upsertFeed($feed)
{
return;
}


public function upsertUserAgents($feed, array $userAgentData, int $trackingDate)
{
return;
}

// FIXME
public function trackEpisodeMatomo($feed, $episode): void
{
if ($feed->podcasterMatomoEnabled()->isTrue()) {
$matomoUtils = new PodcasterStatsMatomo($feed);
$matomoUtils->trackEpisodeDownload($feed, $episode);
}
}

public function trackEpisodeMatomo(): void
public function trackFeedMatomo($feed): void
{
//if ($podcast->podcasterMatomoEnabled()->isTrue()) {
// $matomoUtils = new PodcasterStatsMatomo($podcast->podcasterMatomoSiteId());
// $matomoUtils->trackEpisodeDownload($podcast, $episode);
//}
if ($feed->podcasterMatomoEnabled()->isTrue()) {
$matomoUtils = new PodcasterStatsMatomo($feed);
$matomoUtils->trackFeedDownload($feed);
}
}

// FIXME
public function trackPodTrac(): void
public function trackPodTrac($feed, $episode): void
{
//if ($podcast->podTracEnabled()->isTrue()) {
// $podTrack = new PodcasterStatsPodTrac();
// $podTrack->increaseDownloads($podcast, $episode);
//}
if ($feed->podTracEnabled()->isTrue()) {
$podcast = new Podcast();
$audioFile = $podcast->getAudioFile($episode);
$episodeBaseUrl = str_replace(['https://', 'http://'], ['', ''], $episode->url());
$podTracBaseUrl = rtrim($feed->podTracUrl(), '/');
$podTracUrl = $podTracBaseUrl . '/' . $episodeBaseUrl . '/' . option('mauricerenck.podcaster.downloadTriggerPath', 'download') . '/' . $audioFile->filename();

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $podTracUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_close($ch);
}
}

public function getUserAgent(string $userAgent): array
Expand Down
60 changes: 60 additions & 0 deletions app/PodcasterStatsMatomo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace mauricerenck\Podcaster;
use MatomoTracker;

class PodcasterStatsMatomo
{
private $matomo;

public function __construct($feed)
{
if(!option('mauricerenck.podcaster.matomoBaseUrl', false) || !option('mauricerenck.podcaster.matomoToken', false)) {
return;
}

$this->matomo = new MatomoTracker((int)$feed->podcasterMatomoSiteId(), option('mauricerenck.podcaster.matomoBaseUrl'));
$this->matomo->setTokenAuth(option('mauricerenck.podcaster.matomoToken'));
$this->matomo->disableSendImageResponse();
$this->matomo->disableCookieSupport();
$this->matomo->setIp($_SERVER['REMOTE_ADDR']);
}

public function trackEpisodeDownload($feed, $episode)
{
$this->matomo->setUrl($episode->url());

if ($feed->podcasterMatomoGoalId()->isNotEmpty()) {
$this->matomo->doTrackGoal($feed->podcasterMatomoGoalId(), 1);
}

if ($feed->podcasterMatomoEventName()->isNotEmpty()) {
$this->matomo->doTrackEvent($feed->podcasterTitle(), $episode->title(), $feed->podcasterMatomoEventName());
}

if ($feed->podcasterMatomoAction()->isTrue()) {
$this->matomo->doTrackAction($episode->url(), 'download');
}
}

public function trackFeedDownload($feed)
{
$this->matomo->setUrl($feed->url());

if ($feed->podcasterMatomoFeedPage()->isNotEmpty() && $feed->podcasterMatomoFeedPage()->isTrue()) {
$this->matomo->doTrackPageView($feed->podcasterTitle());
}

if ($feed->podcasterMatomoFeedGoalId()->isNotEmpty()) {
$this->matomo->doTrackGoal($feed->podcasterMatomoFeedGoalId(), 1);
}

if ($feed->podcasterMatomoFeedEventName()->isNotEmpty()) {
$this->matomo->doTrackEvent($feed->podcasterTitle(), $feed->podcasterMatomoFeedEventName(), 1);
}

if ($feed->podcasterMatomoFeedAction()->isTrue()) {
$this->matomo->doTrackAction($feed->url(), 'download');
}
}
}
2 changes: 1 addition & 1 deletion app/PodcasterStatsMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct()
$this->database = $podcasterDb->connect('mysql');
}

public function trackFeed($feed): void
public function upsertFeed($feed): void
{
[$fields, $values] = $this->getFeedQueryData($feed);

Expand Down
2 changes: 1 addition & 1 deletion app/PodcasterStatsSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct()
$this->database = $podcasterDb->connect('sqlite');
}

public function trackFeed($feed): void
public function upsertFeed($feed): void
{
[$fields, $values] = $this->getFeedQueryData($feed);

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require": {
"getkirby/composer-installer": "^1.2",
"james-heinrich/getid3": "^v1.9",
"matomo/matomo-php-tracker": "^3",
"matomo/matomo-php-tracker": "^3.2",
"php": ">=8.0.0"
},
"require-dev": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions migrations/mysql_202305121150.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE episodes ADD COLUMN file_size bigint NULL;
1 change: 1 addition & 0 deletions migrations/sqlite_202305121150.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "episodes" ADD COLUMN "file_size" bigint;

0 comments on commit dc8f44b

Please sign in to comment.