findOrFail($id); // Session key: authenticated user ID or hashed IP $sessionKey = $request->user() ? 'u:' . $request->user()->id : 'ip:' . md5($request->ip()); $counted = $this->analytics->trackImpression($post, $sessionKey); return response()->json(['counted' => $counted]); } public function show(Request $request, int $id): JsonResponse { $post = Post::findOrFail($id); // Only the post owner can view analytics if ($request->user()?->id !== $post->user_id) { abort(403, 'You do not own this post.'); } return response()->json(['data' => $this->analytics->getSummary($post)]); } }