Click.php
5 months ago
Event.php
5 months ago
Order.php
5 months ago
Origin.php
5 months ago
Submission.php
5 months ago
View.php
3 months ago
Click.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Journey\Events; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Illuminate_Builder; |
| 7 | use IAWP\Tables; |
| 8 | use IAWP\Utils\Security; |
| 9 | use IAWP\Utils\Timezone; |
| 10 | use IAWP\Utils\URL; |
| 11 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 12 | /** @internal */ |
| 13 | class Click extends \IAWP\Journey\Events\Event |
| 14 | { |
| 15 | private int $session_id; |
| 16 | private string $created_at; |
| 17 | private int $click_id; |
| 18 | private string $target; |
| 19 | private array $rules; |
| 20 | public function __construct(object $record) |
| 21 | { |
| 22 | $this->session_id = $record->session_id; |
| 23 | $this->created_at = $record->created_at; |
| 24 | $this->click_id = $record->click_id; |
| 25 | $this->target = $record->target; |
| 26 | $this->rules = $record->rules; |
| 27 | } |
| 28 | public function type() : string |
| 29 | { |
| 30 | return 'click'; |
| 31 | } |
| 32 | public function label() : string |
| 33 | { |
| 34 | return \__('Click', 'independent-analytics'); |
| 35 | } |
| 36 | public function created_at() : ?CarbonImmutable |
| 37 | { |
| 38 | return CarbonImmutable::parse($this->created_at, 'utc')->timezone(Timezone::site_timezone()); |
| 39 | } |
| 40 | public function html() : string |
| 41 | { |
| 42 | return \IAWPSCOPED\iawp_render('journeys.timeline.click', ['event' => $this]); |
| 43 | } |
| 44 | public function target() : string |
| 45 | { |
| 46 | return $this->target; |
| 47 | } |
| 48 | public function is_url_target() : bool |
| 49 | { |
| 50 | $url = new URL($this->target); |
| 51 | return $url->is_valid_url(); |
| 52 | } |
| 53 | public function rules() : array |
| 54 | { |
| 55 | return $this->rules; |
| 56 | } |
| 57 | public function rule() : string |
| 58 | { |
| 59 | return Arr::first($this->rules); |
| 60 | } |
| 61 | public function has_multiple_rules() : bool |
| 62 | { |
| 63 | return \count($this->rules) > 1; |
| 64 | } |
| 65 | public static function from_session(int $session_id) : array |
| 66 | { |
| 67 | $query = Illuminate_Builder::new()->select(['sessions.session_id', 'clicks.click_id', 'clicks.created_at', 'click_targets.target', 'click_targets.protocol', 'link_rules.name'])->from(Tables::sessions(), 'sessions')->join(Tables::views() . ' AS views', 'sessions.session_id', '=', 'views.session_id')->join(Tables::clicks() . ' AS clicks', 'views.id', '=', 'clicks.view_id')->join(Tables::clicked_links() . ' AS clicked_links', 'clicks.click_id', '=', 'clicked_links.click_id')->join(Tables::links() . ' AS links', 'clicked_links.link_id', '=', 'links.id')->join(Tables::click_targets() . ' AS click_targets', 'links.click_target_id', '=', 'click_targets.click_target_id')->join(Tables::link_rules() . ' AS link_rules', 'links.link_rule_id', '=', 'link_rules.link_rule_id')->where('sessions.session_id', '=', $session_id); |
| 68 | $records = $query->get()->groupBy('click_id')->map(function ($group) { |
| 69 | $first = $group->first(); |
| 70 | if ($first->protocol === 'tel' || $first->protocol === 'sms') { |
| 71 | $first->target = Security::string($first->target); |
| 72 | } |
| 73 | return ['session_id' => $first->session_id, 'click_id' => $first->click_id, 'created_at' => $first->created_at, 'target' => $first->target, 'rules' => $group->pluck('name')->unique()->values()->all()]; |
| 74 | })->all(); |
| 75 | return \array_map(function ($record) { |
| 76 | return new \IAWP\Journey\Events\Click((object) $record); |
| 77 | }, $records); |
| 78 | } |
| 79 | } |
| 80 |