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
Origin.php
163 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Journey\Events; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Icon_Directory_Factory; |
| 7 | use IAWP\Illuminate_Builder; |
| 8 | use IAWP\Tables; |
| 9 | use IAWP\Utils\Obj; |
| 10 | use IAWP\Utils\Timezone; |
| 11 | use IAWP\Utils\URL; |
| 12 | /** @internal */ |
| 13 | class Origin extends \IAWP\Journey\Events\Event |
| 14 | { |
| 15 | private int $session_id; |
| 16 | private int $visitor_id; |
| 17 | private string $created_at; |
| 18 | private int $session_count; |
| 19 | private ?string $referrer; |
| 20 | private ?string $domain; |
| 21 | private ?string $referrer_type; |
| 22 | private ?string $utm_term; |
| 23 | private ?string $utm_content; |
| 24 | private ?string $utm_campaign; |
| 25 | private ?string $utm_source; |
| 26 | private ?string $utm_medium; |
| 27 | private ?string $country; |
| 28 | private ?string $country_code; |
| 29 | private ?string $device_type; |
| 30 | private ?string $device_browser; |
| 31 | public function __construct(object $record) |
| 32 | { |
| 33 | $this->session_id = $record->session_id; |
| 34 | $this->visitor_id = $record->visitor_id; |
| 35 | $this->created_at = $record->created_at; |
| 36 | $this->session_count = $record->session_count; |
| 37 | $this->referrer = $record->referrer ?? null; |
| 38 | $this->domain = $record->domain ?? null; |
| 39 | $this->referrer_type = $record->referrer_type ?? null; |
| 40 | $this->utm_term = $record->utm_term ?? null; |
| 41 | $this->utm_content = $record->utm_content ?? null; |
| 42 | $this->utm_campaign = $record->utm_campaign ?? null; |
| 43 | $this->utm_source = $record->utm_source ?? null; |
| 44 | $this->utm_medium = $record->utm_medium ?? null; |
| 45 | $this->country = $record->country ?? null; |
| 46 | $this->country_code = $record->country_code ?? null; |
| 47 | $this->device_type = $record->device_type ?? null; |
| 48 | $this->device_browser = $record->device_browser ?? null; |
| 49 | } |
| 50 | public function type() : string |
| 51 | { |
| 52 | return 'origin'; |
| 53 | } |
| 54 | public function label() : string |
| 55 | { |
| 56 | if ($this->referrer === null) { |
| 57 | return \__('Origin', 'independent-analytics'); |
| 58 | } |
| 59 | return $this->referrer; |
| 60 | } |
| 61 | public function referrer_url() : ?string |
| 62 | { |
| 63 | if ($this->domain === null) { |
| 64 | return null; |
| 65 | } |
| 66 | $value = \sanitize_url($this->domain, ['https']); |
| 67 | $url = new URL($value); |
| 68 | if (!$url->is_valid_url()) { |
| 69 | return null; |
| 70 | } |
| 71 | return $url->get_url(); |
| 72 | } |
| 73 | public function created_at() : ?CarbonImmutable |
| 74 | { |
| 75 | return CarbonImmutable::parse($this->created_at, 'utc')->timezone(Timezone::site_timezone()); |
| 76 | } |
| 77 | public function html() : string |
| 78 | { |
| 79 | return \IAWPSCOPED\iawp_render('journeys.timeline.origin', ['event' => $this]); |
| 80 | } |
| 81 | public function has_utm_parameters() : bool |
| 82 | { |
| 83 | $properties = [$this->utm_term, $this->utm_content, $this->utm_campaign, $this->utm_source, $this->utm_medium]; |
| 84 | foreach ($properties as $property) { |
| 85 | if (\is_string($property)) { |
| 86 | return \true; |
| 87 | } |
| 88 | } |
| 89 | return \false; |
| 90 | } |
| 91 | public function utm_term() : ?string |
| 92 | { |
| 93 | return $this->utm_term; |
| 94 | } |
| 95 | public function utm_content() : ?string |
| 96 | { |
| 97 | return $this->utm_content; |
| 98 | } |
| 99 | public function utm_campaign() : ?string |
| 100 | { |
| 101 | return $this->utm_campaign; |
| 102 | } |
| 103 | public function utm_source() : ?string |
| 104 | { |
| 105 | return $this->utm_source; |
| 106 | } |
| 107 | public function utm_medium() : ?string |
| 108 | { |
| 109 | return $this->utm_medium; |
| 110 | } |
| 111 | public function visitor_id() : int |
| 112 | { |
| 113 | return $this->visitor_id; |
| 114 | } |
| 115 | public function country() : ?string |
| 116 | { |
| 117 | return $this->country; |
| 118 | } |
| 119 | public function country_flag_url() : ?string |
| 120 | { |
| 121 | if ($this->country_code === null) { |
| 122 | return null; |
| 123 | } |
| 124 | return Icon_Directory_Factory::flags()->find_icon_url($this->country_code); |
| 125 | } |
| 126 | public function device_type() : ?string |
| 127 | { |
| 128 | return $this->device_type; |
| 129 | } |
| 130 | public function session_count() : int |
| 131 | { |
| 132 | return $this->session_count; |
| 133 | } |
| 134 | public function device_type_url() : ?string |
| 135 | { |
| 136 | if ($this->device_type === null) { |
| 137 | return null; |
| 138 | } |
| 139 | return Icon_Directory_Factory::device_types()->find_icon_url($this->device_type); |
| 140 | } |
| 141 | public function device_browser() : ?string |
| 142 | { |
| 143 | return $this->device_browser; |
| 144 | } |
| 145 | public function device_browser_url() : ?string |
| 146 | { |
| 147 | if ($this->device_browser === null) { |
| 148 | return null; |
| 149 | } |
| 150 | return Icon_Directory_Factory::browsers()->find_icon_url($this->device_browser); |
| 151 | } |
| 152 | public static function from_session(int $session_id) : ?self |
| 153 | { |
| 154 | $session_count = Illuminate_Builder::new()->selectRaw('count(*)')->from(Tables::sessions(), 'visitor_sessions')->whereColumn('visitor_sessions.visitor_id', 'sessions.visitor_id'); |
| 155 | $query = Illuminate_Builder::new()->select(['sessions.session_id', 'sessions.visitor_id', 'sessions.created_at', 'referrers.referrer', 'referrers.domain', 'referrer_types.referrer_type', 'campaigns.utm_term', 'campaigns.utm_content', 'utm_campaigns.utm_campaign', 'utm_sources.utm_source', 'utm_mediums.utm_medium', 'countries.country_code', 'countries.country', 'device_types.device_type', 'device_browsers.device_browser'])->selectSub($session_count, 'session_count')->from(Tables::sessions(), 'sessions')->leftJoin(Tables::referrers() . ' AS referrers', 'sessions.referrer_id', '=', 'referrers.id')->leftJoin(Tables::referrer_types() . ' AS referrer_types', 'referrers.referrer_type_id', '=', 'referrer_types.id')->leftJoin(Tables::campaigns() . ' AS campaigns', 'sessions.campaign_id', '=', 'campaigns.campaign_id')->leftJoin(Tables::utm_campaigns() . ' AS utm_campaigns', 'campaigns.utm_campaign_id', '=', 'utm_campaigns.id')->leftJoin(Tables::utm_sources() . ' AS utm_sources', 'campaigns.utm_source_id', '=', 'utm_sources.id')->leftJoin(Tables::utm_mediums() . ' AS utm_mediums', 'campaigns.utm_medium_id', '=', 'utm_mediums.id')->leftJoin(Tables::countries() . ' AS countries', 'sessions.country_id', '=', 'countries.country_id')->leftJoin(Tables::device_types() . ' AS device_types', 'sessions.device_type_id', '=', 'device_types.device_type_id')->leftJoin(Tables::device_browsers() . ' AS device_browsers', 'sessions.device_browser_id', '=', 'device_browsers.device_browser_id')->where('sessions.session_id', '=', $session_id); |
| 156 | $record = $query->first(); |
| 157 | if ($record === null) { |
| 158 | return null; |
| 159 | } |
| 160 | return new \IAWP\Journey\Events\Origin(Obj::empty_strings_to_null($record)); |
| 161 | } |
| 162 | } |
| 163 |