Skip to content

Commit 6d45fa5

Browse files
authored
Add analytics support (#128)
1 parent 5cce72a commit 6d45fa5

File tree

2 files changed

+134
-3
lines changed

2 files changed

+134
-3
lines changed

stream/client.py

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ def get_default_header(self):
133133

134134
def get_full_url(self, service_name, relative_url):
135135
if self.api_location:
136-
hostname = "%s-%s.%s" % (
136+
hostname = "%s%s.%s" % (
137137
self.api_location,
138-
service_name,
138+
"" if service_name == "analytics" else f"-{service_name}",
139139
self.base_domain_name,
140140
)
141141
elif service_name:
@@ -472,7 +472,7 @@ def create_redirect_url(self, target_url, user_id, events):
472472
params = dict(auth_type="jwt", authorization=auth_token, url=target_url)
473473
params["api_key"] = self.api_key
474474
params["events"] = json.dumps(events)
475-
url = self.base_analytics_url + "redirect/"
475+
url = f"{self.base_analytics_url}redirect/"
476476
# we get the url from the prepare request, this skips issues with
477477
# python's urlencode implementation
478478
request = Request("GET", url, params=params)
@@ -481,6 +481,76 @@ def create_redirect_url(self, target_url, user_id, events):
481481
Request("GET", target_url).prepare()
482482
return prepared_request.url
483483

484+
def track_engagements(self, engagements):
485+
"""
486+
Creates a list of engagements
487+
488+
;param engagements: Slice of engagements to create.
489+
490+
eg.
491+
[
492+
{
493+
"content": "1",
494+
"label": "click",
495+
"features": [
496+
{"group": "topic", "value": "js"},
497+
{"group": "user", "value": "tommaso"},
498+
],
499+
"user_data": "tommaso",
500+
},
501+
{
502+
"content": "2",
503+
"label": "click",
504+
"features": [
505+
{"group": "topic", "value": "go"},
506+
{"group": "user", "value": "tommaso"},
507+
],
508+
"user_data": {"id": "486892", "alias": "Julian"},
509+
},
510+
{
511+
"content": "3",
512+
"label": "click",
513+
"features": [{"group": "topic", "value": "go"}],
514+
"user_data": {"id": "tommaso", "alias": "tommaso"},
515+
},
516+
]
517+
"""
518+
519+
auth_token = self.create_jwt_token("*", "*", feed_id="*")
520+
self.post(
521+
"engagement/",
522+
auth_token,
523+
data={"content_list": engagements},
524+
service_name="analytics",
525+
)
526+
527+
def track_impressions(self, impressions):
528+
"""
529+
Creates a list of impressions
530+
531+
;param impressions: Slice of impressions to create.
532+
533+
eg.
534+
[
535+
{
536+
"content_list": ["1", "2", "3"],
537+
"features": [
538+
{"group": "topic", "value": "js"},
539+
{"group": "user", "value": "tommaso"},
540+
],
541+
"user_data": {"id": "tommaso", "alias": "tommaso"},
542+
},
543+
{
544+
"content_list": ["2", "3", "5"],
545+
"features": [{"group": "topic", "value": "js"}],
546+
"user_data": {"id": "486892", "alias": "Julian"},
547+
},
548+
]
549+
"""
550+
551+
auth_token = self.create_jwt_token("*", "*", feed_id="*")
552+
self.post("impression/", auth_token, data=impressions, service_name="analytics")
553+
484554
def og(self, target_url):
485555
"""
486556
Retrieve open graph information from a URL which you can

stream/tests/test_client.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ def test_collections_url(self):
126126
feed_url, "https://qa-api.stream-io-api.com/api/v1.0/meta/"
127127
)
128128

129+
def test_analytics_url(self):
130+
feed_url = client.get_full_url(
131+
relative_url="engagement/", service_name="analytics"
132+
)
133+
134+
if self.local_tests:
135+
self.assertEqual(
136+
feed_url, "http://localhost:8000/analytics/v1.0/engagement/"
137+
)
138+
else:
139+
self.assertEqual(
140+
feed_url, "https://qa.stream-io-api.com/analytics/v1.0/engagement/"
141+
)
142+
129143
def test_personalization_url(self):
130144
feed_url = client.get_full_url(
131145
relative_url="recommended", service_name="personalization"
@@ -1616,6 +1630,53 @@ def test_feed_enrichment_reaction_counts(self):
16161630
enriched_response = f.get(reactions={"counts": True})
16171631
self.assertEqual(enriched_response["results"][0]["reaction_counts"]["like"], 1)
16181632

1633+
def test_track_engagements(self):
1634+
engagements = [
1635+
{
1636+
"content": "1",
1637+
"label": "click",
1638+
"features": [
1639+
{"group": "topic", "value": "js"},
1640+
{"group": "user", "value": "tommaso"},
1641+
],
1642+
"user_data": "tommaso",
1643+
},
1644+
{
1645+
"content": "2",
1646+
"label": "click",
1647+
"features": [
1648+
{"group": "topic", "value": "go"},
1649+
{"group": "user", "value": "tommaso"},
1650+
],
1651+
"user_data": {"id": "486892", "alias": "Julian"},
1652+
},
1653+
{
1654+
"content": "3",
1655+
"label": "click",
1656+
"features": [{"group": "topic", "value": "go"}],
1657+
"user_data": {"id": "tommaso", "alias": "tommaso"},
1658+
},
1659+
]
1660+
client.track_engagements(engagements)
1661+
1662+
def test_track_impressions(self):
1663+
impressions = [
1664+
{
1665+
"content_list": ["1", "2", "3"],
1666+
"features": [
1667+
{"group": "topic", "value": "js"},
1668+
{"group": "user", "value": "tommaso"},
1669+
],
1670+
"user_data": {"id": "tommaso", "alias": "tommaso"},
1671+
},
1672+
{
1673+
"content_list": ["2", "3", "5"],
1674+
"features": [{"group": "topic", "value": "js"}],
1675+
"user_data": {"id": "486892", "alias": "Julian"},
1676+
},
1677+
]
1678+
client.track_impressions(impressions)
1679+
16191680
def test_og(self):
16201681
response = client.og("https://google.com")
16211682
self.assertTrue("title" in response)

0 commit comments

Comments
 (0)