2020-06-12 20:41:09 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-12 20:41:09 +00:00
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Class AnalyticsWebsiteByRefererModel Model for analytics_website_by_referer table in database
|
|
|
|
*
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-12 20:41:09 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-08-23 11:05:16 +00:00
|
|
|
namespace Modules\Analytics\Models;
|
2020-06-12 20:41:09 +00:00
|
|
|
|
|
|
|
use CodeIgniter\Model;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Analytics\Entities\AnalyticsWebsiteByReferer;
|
2020-06-12 20:41:09 +00:00
|
|
|
|
|
|
|
class AnalyticsWebsiteByRefererModel extends Model
|
|
|
|
{
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-06-12 20:41:09 +00:00
|
|
|
protected $table = 'analytics_website_by_referer';
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $returnType = AnalyticsWebsiteByReferer::class;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-06-12 20:41:09 +00:00
|
|
|
protected $useSoftDeletes = false;
|
|
|
|
|
2021-05-06 14:00:48 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2020-06-12 20:41:09 +00:00
|
|
|
protected $useTimestamps = false;
|
2020-10-08 14:45:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets referer data for a podcast
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return AnalyticsWebsiteByReferer[]
|
2020-10-08 14:45:46 +00:00
|
|
|
*/
|
|
|
|
public function getData(int $podcastId): array
|
|
|
|
{
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($found = cache("{$podcastId}_analytics_website_by_referer"))) {
|
2020-11-03 14:14:30 +00:00
|
|
|
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
|
2021-04-02 17:20:02 +00:00
|
|
|
$found = $this->select('referer_url as labels')
|
|
|
|
->selectSum('hits', 'values')
|
2020-10-08 14:45:46 +00:00
|
|
|
->where([
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'date >' => $oneWeekAgo,
|
2020-10-08 14:45:46 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 14:14:30 +00:00
|
|
|
->findAll();
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-06-08 09:52:11 +00:00
|
|
|
->save("{$podcastId}_analytics_website_by_referer", $found, 600);
|
2020-10-08 14:45:46 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets domain data for a podcast
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return AnalyticsWebsiteByReferer[]
|
2020-10-08 14:45:46 +00:00
|
|
|
*/
|
2020-10-14 10:38:48 +00:00
|
|
|
public function getDataByDomainWeekly(int $podcastId): array
|
2020-10-08 14:45:46 +00:00
|
|
|
{
|
2020-10-14 10:38:48 +00:00
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($found = cache("{$podcastId}_analytics_website_by_domain_weekly"))
|
2020-10-14 10:38:48 +00:00
|
|
|
) {
|
2020-11-03 14:14:30 +00:00
|
|
|
$oneWeekAgo = date('Y-m-d', strtotime('-1 week'));
|
2021-05-06 14:00:48 +00:00
|
|
|
$found = $this->select("SUBSTRING_INDEX(domain, '.', -2) as labels")
|
2021-04-02 17:20:02 +00:00
|
|
|
->selectSum('hits', 'values')
|
2020-10-08 14:45:46 +00:00
|
|
|
->where([
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'date >' => $oneWeekAgo,
|
2020-10-08 14:45:46 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 14:14:30 +00:00
|
|
|
->findAll();
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-06-08 09:52:11 +00:00
|
|
|
->save("{$podcastId}_analytics_website_by_domain_weekly", $found, 600);
|
2020-10-14 10:38:48 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2020-10-14 10:38:48 +00:00
|
|
|
return $found;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets domain data for a podcast
|
|
|
|
*
|
2021-05-06 14:00:48 +00:00
|
|
|
* @return AnalyticsWebsiteByReferer[]
|
2020-10-14 10:38:48 +00:00
|
|
|
*/
|
|
|
|
public function getDataByDomainYearly(int $podcastId): array
|
|
|
|
{
|
|
|
|
if (
|
2021-05-19 16:35:13 +00:00
|
|
|
! ($found = cache("{$podcastId}_analytics_website_by_domain_yearly"))
|
2020-10-14 10:38:48 +00:00
|
|
|
) {
|
2020-11-03 14:14:30 +00:00
|
|
|
$oneYearAgo = date('Y-m-d', strtotime('-1 year'));
|
2021-05-06 14:00:48 +00:00
|
|
|
$found = $this->select("SUBSTRING_INDEX(domain, '.', -2) as labels")
|
2021-04-02 17:20:02 +00:00
|
|
|
->selectSum('hits', 'values')
|
2020-10-14 10:38:48 +00:00
|
|
|
->where([
|
2021-04-02 17:20:02 +00:00
|
|
|
'podcast_id' => $podcastId,
|
|
|
|
'date >' => $oneYearAgo,
|
2020-10-14 10:38:48 +00:00
|
|
|
])
|
2021-04-02 17:20:02 +00:00
|
|
|
->groupBy('labels')
|
|
|
|
->orderBy('values', 'DESC')
|
2020-11-03 14:14:30 +00:00
|
|
|
->findAll();
|
2021-05-19 16:35:13 +00:00
|
|
|
cache()
|
2021-06-08 09:52:11 +00:00
|
|
|
->save("{$podcastId}_analytics_website_by_domain_yearly", $found, 600);
|
2020-10-08 14:45:46 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2020-10-08 14:45:46 +00:00
|
|
|
return $found;
|
|
|
|
}
|
2020-06-12 20:41:09 +00:00
|
|
|
}
|