PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.14.8
Independent Analytics – WordPress Analytics Plugin v2.14.8
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 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