Skip to content

Commit

Permalink
Merge pull request #161 from readthedocs/record-views
Browse files Browse the repository at this point in the history
Record views for ad network for now
  • Loading branch information
ericholscher committed Jul 6, 2020
2 parents ab14823 + 2e3012c commit ee36cdf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion adserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,8 @@ def track_view(self, request, publisher, url):
"""
self.incr(VIEWS, publisher)

if settings.ADSERVER_RECORD_VIEWS:
# TODO: Find a better way to record explicit ad server views
if settings.ADSERVER_RECORD_VIEWS or "readthedocs" not in publisher.slug:
self._record_base(request, View, publisher, url)
else:
log.debug("Not recording ad view (settings.ADSERVER_RECORD_VIEWS=False)")
Expand Down
35 changes: 35 additions & 0 deletions adserver/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ def test_ad_click_and_tracking(self):

@override_settings(ADSERVER_RECORD_VIEWS=False)
def test_record_views_false(self):
self.publisher1.slug = "readthedocs-test"
self.publisher1.save()
data = {"placements": self.placements, "publisher": self.publisher1.slug}
resp = self.client.post(
self.url, json.dumps(data), content_type="application/json"
Expand Down Expand Up @@ -847,6 +849,39 @@ def test_record_views_false(self):
).exists()
)

@override_settings(ADSERVER_RECORD_VIEWS=False)
def test_record_views_ad_network(self):
self.publisher1.slug = "another-publisher" # No `readthedocs`
self.publisher1.unauthed_ad_decisions = True # In case we change logic
self.publisher1.save()
data = {"placements": self.placements, "publisher": self.publisher1.slug}
resp = self.client.post(
self.url, json.dumps(data), content_type="application/json"
)
self.assertEqual(resp.status_code, 200, resp.content)
nonce = resp.json()["nonce"]

# Simulate an ad view and verify it was viewed
view_url = reverse(
"view-proxy", kwargs={"advertisement_id": self.ad.pk, "nonce": nonce}
)

resp = self.proxy_client.get(view_url)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["X-Adserver-Reason"], "Billed view")

# Verify an impression was written
impression = self.ad.impressions.filter(publisher=self.publisher1).first()
self.assertEqual(impression.offers, 1)
self.assertEqual(impression.views, 1)

# Make sure we're writing ads for ad network views
self.assertTrue(
View.objects.filter(
advertisement=self.ad, publisher=self.publisher1
).exists()
)


class TestProxyViews(BaseApiTest):
def setUp(self):
Expand Down

0 comments on commit ee36cdf

Please sign in to comment.