Click.php
8 months ago
Click_Processing_Job.php
5 months ago
Config_File_Manager.php
1 year ago
Link_Rule.php
1 year ago
Link_Rule_Finder.php
9 months ago
Click.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Click_Tracking; |
| 4 | |
| 5 | use IAWP\Illuminate_Builder; |
| 6 | use IAWP\Tables; |
| 7 | use IAWPSCOPED\Illuminate\Database\Query\Builder; |
| 8 | use IAWPSCOPED\Illuminate\Support\Str; |
| 9 | /** @internal */ |
| 10 | class Click |
| 11 | { |
| 12 | private $protocol; |
| 13 | private $value; |
| 14 | private $href; |
| 15 | private $classes; |
| 16 | private $ids; |
| 17 | private $resource_id; |
| 18 | private $visitor_id; |
| 19 | private $created_at; |
| 20 | private function __construct(array $record) |
| 21 | { |
| 22 | $this->protocol = $this->extract_protocol_from_href($record['href']); |
| 23 | $this->value = $this->extract_target_value_from_href($record['href']); |
| 24 | $this->href = $record['href']; |
| 25 | $this->classes = $record['classes']; |
| 26 | $this->ids = $record['ids']; |
| 27 | $this->resource_id = $record['resource_id']; |
| 28 | $this->visitor_id = $record['visitor_id']; |
| 29 | $this->created_at = $record['created_at']; |
| 30 | } |
| 31 | /** |
| 32 | * Update the database |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | public function track() : void |
| 37 | { |
| 38 | $link_rules = \IAWP\Click_Tracking\Link_Rule_Finder::new($this->protocol, $this->href, $this->classes, $this->ids)->links(); |
| 39 | if ($link_rules->isEmpty()) { |
| 40 | return; |
| 41 | } |
| 42 | $view_id = $this->get_view_id(); |
| 43 | if (\is_null($view_id)) { |
| 44 | return; |
| 45 | } |
| 46 | $click_target = $this->get_click_target(); |
| 47 | $link_ids = $link_rules->map(function (\IAWP\Click_Tracking\Link_Rule $link_rule) use($click_target) { |
| 48 | return $this->get_link_id_for($link_rule->id(), $click_target->click_target_id); |
| 49 | }); |
| 50 | $click_id = Illuminate_Builder::new()->from(Tables::clicks())->insertGetId(['view_id' => $view_id, 'created_at' => $this->created_at->format('Y-m-d H:i:s')]); |
| 51 | $link_ids->each(function ($link_id) use($click_id) { |
| 52 | Illuminate_Builder::new()->from(Tables::clicked_links())->insertGetId(['click_id' => $click_id, 'link_id' => $link_id]); |
| 53 | }); |
| 54 | } |
| 55 | private function extract_protocol_from_href(?string $href) : ?string |
| 56 | { |
| 57 | if (\is_null($href)) { |
| 58 | return null; |
| 59 | } |
| 60 | if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) { |
| 61 | return Str::before($href, ':'); |
| 62 | } |
| 63 | return null; |
| 64 | } |
| 65 | private function extract_target_value_from_href(?string $href) : ?string |
| 66 | { |
| 67 | if (\is_null($href)) { |
| 68 | return null; |
| 69 | } |
| 70 | if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) { |
| 71 | return Str::after($href, ':'); |
| 72 | } |
| 73 | return $href; |
| 74 | } |
| 75 | private function get_click_target() : object |
| 76 | { |
| 77 | $select_query = Illuminate_Builder::new()->from(Tables::click_targets())->where('target', '=', $this->value)->when(\is_null($this->protocol), function (Builder $query) { |
| 78 | $query->whereNull('protocol'); |
| 79 | })->when(\is_string($this->protocol), function (Builder $query) { |
| 80 | $query->where('protocol', '=', $this->protocol); |
| 81 | }); |
| 82 | $match = $select_query->first(); |
| 83 | if (\is_object($match)) { |
| 84 | return $match; |
| 85 | } |
| 86 | Illuminate_Builder::new()->from(Tables::click_targets())->insertOrIgnore(['target' => $this->value, 'protocol' => $this->protocol]); |
| 87 | return $select_query->first(); |
| 88 | } |
| 89 | private function get_view_id() : ?int |
| 90 | { |
| 91 | $sessions_table = Tables::sessions(); |
| 92 | $view_id = Illuminate_Builder::new()->from(Tables::views(), 'views')->join("{$sessions_table} AS sessions", 'views.session_id', '=', 'sessions.session_id')->where('views.resource_id', '=', $this->resource_id)->where('sessions.visitor_id', '=', $this->visitor_id)->where('views.viewed_at', '<=', $this->created_at->format('Y-m-d H:i:s'))->orderByDesc('views.viewed_at')->limit(1)->value('views.id'); |
| 93 | // There's a small chance that view_id is a string instead of an int. In that case, |
| 94 | // it should be converted to a string. |
| 95 | // https://github.com/andrewjmead/independent-analytics/issues/1335 |
| 96 | if (\is_string($view_id)) { |
| 97 | return (int) $view_id; |
| 98 | } |
| 99 | if (\is_int($view_id)) { |
| 100 | return $view_id; |
| 101 | } |
| 102 | return null; |
| 103 | } |
| 104 | private function get_link_id_for(int $link_rule_id, int $click_target_id) : int |
| 105 | { |
| 106 | $select_query = Illuminate_Builder::new()->from(Tables::links())->where('link_rule_id', '=', $link_rule_id)->where('click_target_id', '=', $click_target_id); |
| 107 | $match = $select_query->first(); |
| 108 | if (\is_object($match)) { |
| 109 | return $match->id; |
| 110 | } |
| 111 | Illuminate_Builder::new()->from(Tables::links())->insertOrIgnore(['link_rule_id' => $link_rule_id, 'click_target_id' => $click_target_id]); |
| 112 | return $select_query->first()->id; |
| 113 | } |
| 114 | public static function new(array $record) : ?self |
| 115 | { |
| 116 | return new self($record); |
| 117 | } |
| 118 | } |
| 119 |