|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Models; |
| 4 | + |
| 5 | +use App\Concerns\AnalyticsDateFormatting; |
| 6 | +use App\Concerns\AnonymizesRequests; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Str; |
| 10 | +use Carbon\Carbon; |
| 11 | + |
| 12 | +/** |
| 13 | + * @property string $page URL of the page visited |
| 14 | + * @property ?string $referrer URL of the page that referred the user |
| 15 | + * @property ?string $user_agent User agent string of the visitor (only stored for bots) |
| 16 | + * @property string $anonymous_id Ephemeral anonymized visitor identifier that cannot be tied to a user |
| 17 | + */ |
| 18 | +class PageView extends Model |
| 19 | +{ |
| 20 | + public const UPDATED_AT = null; |
| 21 | + |
| 22 | + protected $fillable = [ |
| 23 | + 'page', |
| 24 | + 'referrer', |
| 25 | + 'user_agent', |
| 26 | + 'anonymous_id', |
| 27 | + ]; |
| 28 | + |
| 29 | + protected static function boot(): void |
| 30 | + { |
| 31 | + parent::boot(); |
| 32 | + |
| 33 | + static::creating(function (self $model): void { |
| 34 | + // Normalize the page URL to use the path only |
| 35 | + $model->page = (parse_url($model->page, PHP_URL_PATH) ?? '/'); |
| 36 | + |
| 37 | + // We only store the domain of the referrer |
| 38 | + if ($model->referrer) { |
| 39 | + if (! str_starts_with($model->referrer, '?ref=')) { |
| 40 | + // We only store the domain of the referrer |
| 41 | + $model->referrer = static::normalizeDomain($model->referrer); |
| 42 | + } else { |
| 43 | + $domain = Str::after($model->referrer, '?ref='); |
| 44 | + $domain = static::normalizeDomain($domain); |
| 45 | + |
| 46 | + $model->referrer = "?ref=$domain"; |
| 47 | + } |
| 48 | + } else { |
| 49 | + $model->referrer = null; |
| 50 | + } |
| 51 | + |
| 52 | + // We don't store user agents for non-bot users |
| 53 | + $crawlerKeywords = ['bot', 'crawl', 'spider', 'slurp', 'search', 'yahoo', 'facebook']; |
| 54 | + |
| 55 | + if (! Str::contains($model->user_agent, $crawlerKeywords, true)) { |
| 56 | + $model->user_agent = null; |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + public static function fromRequest(Request $request): static |
| 62 | + { |
| 63 | + // Is a ref query parameter present? If so, we'll store it as a referrer |
| 64 | + $ref = $request->query('ref'); |
| 65 | + if ($ref) { |
| 66 | + $ref = '?ref='.$ref; |
| 67 | + } |
| 68 | + |
| 69 | + return static::create([ |
| 70 | + 'page' => $request->url(), |
| 71 | + 'referrer' => $ref ?? $request->header('referer') ?? $request->header('referrer'), |
| 72 | + 'user_agent' => $request->userAgent(), |
| 73 | + 'anonymous_id' => self::anonymizeRequest($request), |
| 74 | + ]); |
| 75 | + } |
| 76 | + |
| 77 | + public function getCreatedAtAttribute(string $date): Carbon |
| 78 | + { |
| 79 | + // Include the timezone when casting the date to a string |
| 80 | + return Carbon::parse($date)->settings(['toStringFormat' => 'Y-m-d H:i:s T']); |
| 81 | + } |
| 82 | + |
| 83 | + protected static function normalizeDomain(string $url): string |
| 84 | + { |
| 85 | + if (! Str::startsWith($url, 'http')) { |
| 86 | + $url = 'https://'.$url; |
| 87 | + } |
| 88 | + |
| 89 | + return Str::after(parse_url($url, PHP_URL_HOST), 'www.'); |
| 90 | + } |
| 91 | + |
| 92 | + protected static function anonymizeRequest(Request $request): string |
| 93 | + { |
| 94 | + // As we are not interested in tracking users, we generate an ephemeral hash |
| 95 | + // based on the IP, user agent, and a salt to track unique visits per day. |
| 96 | + // This system is designed so that a visitor cannot be tracked across days, nor can it be tied to a specific person. |
| 97 | + // Due to the salting with a secret environment value, it can't be reverse engineered by creating rainbow tables. |
| 98 | + // The current date is also included in the hash in order to make them unique per day. |
| 99 | + |
| 100 | + // The hash is made using the SHA-256 algorithm and truncated to 40 characters to save space, as we're not too worried about collisions. |
| 101 | + |
| 102 | + $forwardIp = $request->header('X-Forwarded-For'); |
| 103 | + |
| 104 | + if ($forwardIp !== null) { |
| 105 | + // If the request is proxied, we use the first IP in the address list, as the actual IP belongs to the proxy which may change frequently. |
| 106 | + |
| 107 | + $ip = Str::before($forwardIp, ','); |
| 108 | + } else { |
| 109 | + $ip = $request->ip(); |
| 110 | + } |
| 111 | + |
| 112 | + return substr(hash('sha256', $ip.$request->userAgent().config('hashing.anonymizer_salt').now()->format('Y-m-d')), 0, 40); |
| 113 | + } |
| 114 | +} |
0 commit comments