diff --git a/app/PodcasterStats.interface.php b/app/PodcasterStats.interface.php index 62fa1b2..5a00ea5 100644 --- a/app/PodcasterStats.interface.php +++ b/app/PodcasterStats.interface.php @@ -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; @@ -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; diff --git a/app/PodcasterStats.php b/app/PodcasterStats.php index c7c0a6a..bd624d3 100644 --- a/app/PodcasterStats.php +++ b/app/PodcasterStats.php @@ -27,8 +27,22 @@ 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) @@ -36,28 +50,49 @@ 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 diff --git a/app/PodcasterStatsMatomo.php b/app/PodcasterStatsMatomo.php new file mode 100644 index 0000000..4f805ec --- /dev/null +++ b/app/PodcasterStatsMatomo.php @@ -0,0 +1,60 @@ +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'); + } + } +} diff --git a/app/PodcasterStatsMysql.php b/app/PodcasterStatsMysql.php index ae0db1e..10ca634 100644 --- a/app/PodcasterStatsMysql.php +++ b/app/PodcasterStatsMysql.php @@ -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); diff --git a/app/PodcasterStatsSqlite.php b/app/PodcasterStatsSqlite.php index 30134c3..a8a3b92 100644 --- a/app/PodcasterStatsSqlite.php +++ b/app/PodcasterStatsSqlite.php @@ -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); diff --git a/composer.json b/composer.json index 91a913e..24fd81b 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index 446a473..2b955aa 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "04cf6166520a909096a3666637347c6e", + "content-hash": "e95016e30b2c57625288d987a73f237d", "packages": [ { "name": "getkirby/composer-installer", @@ -122,16 +122,16 @@ }, { "name": "matomo/matomo-php-tracker", - "version": "3.1.2", + "version": "3.2.0", "source": { "type": "git", "url": "https://github.com/matomo-org/matomo-php-tracker.git", - "reference": "78932f61692b18c6b5de923dbc3880ad3380e328" + "reference": "c9e7816d63ddf99bffb1414cfbe45f4c4edf1d4b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/matomo-org/matomo-php-tracker/zipball/78932f61692b18c6b5de923dbc3880ad3380e328", - "reference": "78932f61692b18c6b5de923dbc3880ad3380e328", + "url": "https://api.github.com/repos/matomo-org/matomo-php-tracker/zipball/c9e7816d63ddf99bffb1414cfbe45f4c4edf1d4b", + "reference": "c9e7816d63ddf99bffb1414cfbe45f4c4edf1d4b", "shasum": "" }, "require": { @@ -174,7 +174,7 @@ "issues": "https://github.com/matomo-org/matomo-php-tracker/issues", "source": "https://github.com/matomo-org/matomo-php-tracker" }, - "time": "2023-01-18T14:28:44+00:00" + "time": "2023-03-24T15:16:22+00:00" } ], "packages-dev": [ diff --git a/migrations/mysql_202305121150.sql b/migrations/mysql_202305121150.sql new file mode 100644 index 0000000..97380af --- /dev/null +++ b/migrations/mysql_202305121150.sql @@ -0,0 +1 @@ +ALTER TABLE episodes ADD COLUMN file_size bigint NULL; \ No newline at end of file diff --git a/migrations/sqlite_202305121150.sql b/migrations/sqlite_202305121150.sql new file mode 100644 index 0000000..16e6a02 --- /dev/null +++ b/migrations/sqlite_202305121150.sql @@ -0,0 +1 @@ +ALTER TABLE "episodes" ADD COLUMN "file_size" bigint; \ No newline at end of file