Skip to content

[ENG-9907] Analytics are increasing (unique views) when a contributor is viewing their own content (Project) (BE)#11669

Merged
adlius merged 2 commits intoCenterForOpenScience:feature/pbs-26-6from
antkryt:fix/ENG-9907-2
Apr 7, 2026
Merged

[ENG-9907] Analytics are increasing (unique views) when a contributor is viewing their own content (Project) (BE)#11669
adlius merged 2 commits intoCenterForOpenScience:feature/pbs-26-6from
antkryt:fix/ENG-9907-2

Conversation

@antkryt
Copy link
Copy Markdown
Contributor

@antkryt antkryt commented Mar 31, 2026

Ticket

Purpose

do not count contrib views

Changes

Side Effects

QE Notes

CE Notes

Documentation

Comment on lines +361 to +417
class TestContributorExclusion:

def test_creator_pageview_not_recorded(self, app, mock_save):
user = AuthUserFactory()
project = ProjectFactory(creator=user)
payload = counted_usage_payload(
item_guid=project._id,
action_labels=['view', 'web'],
pageview_info={'page_url': f'https://osf.io/{project._id}/'},
)
resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=user.auth)
assert resp.status_code == 204
assert mock_save.call_count == 0

def test_read_contributor_pageview_not_recorded(self, app, mock_save):
creator = AuthUserFactory()
reader = AuthUserFactory()
project = ProjectFactory(creator=creator)
project.add_contributor(reader, permissions=READ, auth=Auth(creator))
payload = counted_usage_payload(
item_guid=project._id,
action_labels=['view', 'web'],
pageview_info={'page_url': f'https://osf.io/{project._id}/analytics/'},
)
resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=reader.auth)
assert resp.status_code == 204
assert mock_save.call_count == 0

def test_non_contributor_pageview_recorded(self, app, mock_save):
creator = AuthUserFactory()
visitor = AuthUserFactory()
project = ProjectFactory(creator=creator, is_public=True)
payload = counted_usage_payload(
item_guid=project._id,
action_labels=['view', 'web'],
pageview_info={'page_url': f'https://osf.io/{project._id}/'},
)
resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=visitor.auth)
assert resp.status_code == 201
assert mock_save.call_count == 1

def test_parent_contributor_not_on_child_component_pageview_recorded(self, app, mock_save):
creator = AuthUserFactory()
child_owner = AuthUserFactory()
parent_reader = AuthUserFactory()
parent = ProjectFactory(creator=creator, is_public=True)
child = NodeFactory(parent=parent, creator=child_owner, is_public=True)
parent.add_contributor(parent_reader, permissions=READ, auth=Auth(creator))
assert not child.contributors_and_group_members.filter(guids___id=parent_reader._id).exists()
payload = counted_usage_payload(
item_guid=child._id,
action_labels=['view', 'web'],
pageview_info={'page_url': f'https://osf.io/{child._id}/'},
)
resp = app.post_json_api(COUNTED_USAGE_URL, payload, auth=parent_reader.auth)
assert resp.status_code == 201
assert mock_save.call_count == 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for a user using a View Only Link to make sure they're counted? Also one for if a user is an admin on a parent project but is not a contributor on the component (like your read contributor one above, but for Admin). For the admin one, I'm not sure that we want to change it whichever way it works out, but I would like to know what to expect there.

Copy link
Copy Markdown
Contributor Author

@antkryt antkryt Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we don't count view ONLY if user is a contributor on the current node (not parent, child, etc): test_parent_contributor_not_on_child_component_pageview_recorded. Before my changes we counted any view (any call to /_/metrics/count) without any checks, so I'm not sure what logic is correct here

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a user views the project using a View Only Link (VOL), does the front end call the /_/metrics/count endpoint with information about the VOL? In other words, would the /_/metrics/count endpoint know that a user is viewing the object using a VOL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frontend is not sending /_/metrics/count for the VOL on staging for some reason, so I can't check the exact payload. I think the only think that is different is page_url which contains ?view_only for VOL. test_anonymous_view_only_link_visitor_pageview_recorded and test_logged_in_non_contributor_view_only_link_pageview_recorded show the expected backend-side logic for anonymous and non-anonymous visitors

@adlius adlius merged commit 8212ede into CenterForOpenScience:feature/pbs-26-6 Apr 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants