PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.10.1
Independent Analytics – WordPress Analytics Plugin v2.10.1
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / Click_Tracking / Click.php
independent-analytics / IAWP / Click_Tracking Last commit date
Click.php 1 year ago Click_Processing_Job.php 1 year ago Config_File_Manager.php 1 year ago Link_Rule.php 1 year ago Link_Rule_Finder.php 1 year ago
Click.php
104 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 $resource_id;
17 private $visitor_id;
18 private $created_at;
19 private function __construct(array $record)
20 {
21 $this->protocol = $this->extract_protocol_from_href($record['href']);
22 $this->value = $this->extract_target_value_from_href($record['href']);
23 $this->href = $record['href'];
24 $this->classes = $record['classes'];
25 $this->resource_id = $record['resource_id'];
26 $this->visitor_id = $record['visitor_id'];
27 $this->created_at = $record['created_at'];
28 }
29 /**
30 * Update the database
31 *
32 * @return void
33 */
34 public function track() : void
35 {
36 $link_rules = \IAWP\Click_Tracking\Link_Rule_Finder::new($this->protocol, $this->href, $this->classes)->links();
37 if ($link_rules->isEmpty()) {
38 return;
39 }
40 $click_target = $this->get_click_target();
41 $view_id = $this->get_view_id();
42 if (\is_null($view_id)) {
43 return;
44 }
45 $click_id = Illuminate_Builder::new()->from(Tables::clicks())->insertGetId(['click_target_id' => $click_target->click_target_id, 'view_id' => $view_id, 'created_at' => $this->created_at->format('Y-m-d H:i:s')]);
46 $link_rules->each(function ($link_rule) use($click_id) {
47 Illuminate_Builder::new()->from(Tables::clicked_links())->insertGetId(['click_id' => $click_id, 'link_rule_id' => $link_rule->id()]);
48 });
49 }
50 private function extract_protocol_from_href(?string $href) : ?string
51 {
52 if (\is_null($href)) {
53 return null;
54 }
55 if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) {
56 return Str::before($href, ':');
57 }
58 return null;
59 }
60 private function extract_target_value_from_href(?string $href) : ?string
61 {
62 if (\is_null($href)) {
63 return null;
64 }
65 if (Str::startsWith($href, ['tel:', 'sms:', 'mailto:'])) {
66 return Str::after($href, ':');
67 }
68 return $href;
69 }
70 private function get_click_target() : object
71 {
72 $select_query = Illuminate_Builder::new()->from(Tables::click_targets())->where('target', '=', $this->value)->when(\is_null($this->protocol), function (Builder $query) {
73 $query->whereNull('protocol');
74 })->when(\is_string($this->protocol), function (Builder $query) {
75 $query->where('protocol', '=', $this->protocol);
76 });
77 $target = $select_query->first();
78 if (\is_object($target)) {
79 return $target;
80 }
81 Illuminate_Builder::new()->from(Tables::click_targets())->insertOrIgnore(['target' => $this->value, 'protocol' => $this->protocol]);
82 return $select_query->first();
83 }
84 private function get_view_id() : ?int
85 {
86 $sessions_table = Tables::sessions();
87 $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');
88 // There's a small chance that view_id is a string instead of an int. In that case,
89 // it should be converted to a string.
90 // https://github.com/andrewjmead/independent-analytics/issues/1335
91 if (\is_string($view_id)) {
92 return (int) $view_id;
93 }
94 if (\is_int($view_id)) {
95 return $view_id;
96 }
97 return null;
98 }
99 public static function new(array $record) : ?self
100 {
101 return new self($record);
102 }
103 }
104