Calculate hits/views for items in collections


#1

Hi I have /groups, /groups/{group_id}, /groups/{group_id}/objects and /groups/{group_id}/objects/{object_id}. Currently I have the server create a “hit” every time a user fetches an endpoint. So for example, GET /groups/{group_id} increments the hit count for the specified group, and GET /groups/{group_id}/objects/{object_id} increments the hit count for the specified object. I used these hits during search to rank groups and objects by popularity.

The problem is that I allow ?include=objects. So the client currently just fetches /groups?include=objects and uses that to build up the models in the app. So when users click on a group to see it, the server has no way of knowing.

Here are a few solutions:

  1. increment the hit count for every item in a collection when the collection is fetched <- not ideal because browsing the main feed increments hits
  2. force the client to fetch groups and objects by id when they are clicked (perhaps by not including attributes in collection responses) <- not ideal because this seems to break the jsonapi spec?
  3. increment the hit count for every item if ?include=objects is present <- not ideal, feels like a special case?
  4. rely on the client to do the right thing and call GET /groups/{group_id} when the user clicks a group <- not ideal, potential for inaccuracy

These all have their drawbacks and I’ve been spinning on this for a few days. I think the issue is that pre-jsonapi I might have only returned group IDs for collection responses, forcing the client to fetch items by id.

Is there something I’m just not seeing? Has anyone else come across this issue?

Thanks!


#2

Which bit of the spec do you think this breaks?

But it is a special case isn’t it? (With the general case being no ?include)[quote=“morris, post:1, topic:475”]
rely on the client to do the right thing and call GET /groups/{group_id} when the user clicks a group
[/quote]

I don’t understand this. Did you mean rely on the client to call GET `/groups/{group_id}/objects when the user clicks on a group?