Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions customerio/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,18 @@ def identify(self, id, **kwargs):
url = self.get_customer_query_string(id)
return self.send_request("PUT", url, kwargs)

def track(self, customer_id, name, **data):
def track(self, customer_id, name, data=None, id=None, timestamp=None):
"""Track an event for a given customer_id."""
if not customer_id:
raise CustomerIOException("customer_id cannot be blank in track")
url = self.get_event_query_string(customer_id)
post_data = {
"name": name,
"data": self._sanitize(data),
}
post_data = self._build_event(name, data, id=id, timestamp=timestamp)
return self.send_request("POST", url, post_data)

def track_anonymous(self, anonymous_id, name, **data):
def track_anonymous(self, anonymous_id, name, data=None, id=None, timestamp=None):
"""Track an event for a given anonymous_id."""
url = self.get_events_query_string()
post_data = {
"name": name,
"data": self._sanitize(data),
}
post_data = self._build_event(name, data, id=id, timestamp=timestamp)
if anonymous_id:
post_data["anonymous_id"] = anonymous_id

Expand Down Expand Up @@ -149,6 +143,27 @@ def backfill(self, customer_id, name, timestamp, **data):

return self.send_request("POST", url, post_data)

def _build_event(self, name, data=None, id=None, timestamp=None):
post_data = {
"name": name,
"data": self._sanitize(data or {}),
}
if id is not None:
post_data["id"] = id
if timestamp is not None:
if isinstance(timestamp, datetime):
timestamp = self._datetime_to_timestamp(timestamp)
elif isinstance(timestamp, int):
pass
else:
try:
timestamp = int(timestamp)
except (ValueError, TypeError):
Comment thread
cursor[bot] marked this conversation as resolved.
Outdated
timestamp = None
if timestamp is not None:
post_data["timestamp"] = timestamp
return post_data

def delete(self, customer_id):
"""Delete a customer profile."""
if not customer_id:
Expand Down
145 changes: 142 additions & 3 deletions tests/test_customerio.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,110 @@ def test_track_call(self):
)
)

self.cio.track(customer_id=1, name="sign_up", email="john@test.com")
self.cio.track(customer_id=1, name="sign_up", data={"email": "john@test.com"})

with self.assertRaises(TypeError):
self.cio.track(random_attr="some_value")

def test_track_with_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {
"name": "purchase",
"data": {"type": "socks"},
"id": "01HB4HBDKTFWYZCK01DMRSWRFD",
},
},
)
)

self.cio.track(1, "purchase", {"type": "socks"}, id="01HB4HBDKTFWYZCK01DMRSWRFD")

def test_track_without_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {"name": "purchase", "data": {"type": "socks"}},
},
)
)

self.cio.track(1, "purchase", {"type": "socks"})

def test_track_with_timestamp(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {
"name": "purchase",
"data": {"type": "socks"},
"timestamp": 1561231234,
},
},
)
)

self.cio.track(1, "purchase", {"type": "socks"}, timestamp=1561231234)

def test_track_with_id_and_timestamp(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {
"name": "purchase",
"data": {"type": "socks"},
"id": "01HB4HBDKTFWYZCK01DMRSWRFD",
"timestamp": 1561231234,
},
},
)
)

self.cio.track(
1, "purchase", {"type": "socks"}, id="01HB4HBDKTFWYZCK01DMRSWRFD", timestamp=1561231234
)

def test_track_with_invalid_timestamp(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {"name": "purchase", "data": {"type": "socks"}},
},
)
)

self.cio.track(1, "purchase", {"type": "socks"}, timestamp="not-a-timestamp")

def test_track_with_no_data(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/customers/1/events",
"body": {"name": "login", "data": {}},
},
)
)

self.cio.track(1, "login")

def test_track_anonymous_call(self):
self.cio.http.hooks = dict(
response=partial(
Expand All @@ -131,7 +230,47 @@ def test_track_anonymous_call(self):
)
)

self.cio.track_anonymous(anonymous_id=123, name="sign_up", email="john@test.com")
self.cio.track_anonymous(anonymous_id=123, name="sign_up", data={"email": "john@test.com"})

def test_track_anonymous_with_id(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/events",
"body": {
"name": "purchase",
"data": {},
"anonymous_id": "anon-123",
"id": "01HB4HBDKTFWYZCK01DMRSWRFD",
},
},
)
)

self.cio.track_anonymous("anon-123", "purchase", id="01HB4HBDKTFWYZCK01DMRSWRFD")

def test_track_anonymous_with_timestamp(self):
self.cio.http.hooks = dict(
response=partial(
self._check_request,
rq={
"method": "POST",
"url_suffix": "/events",
"body": {
"name": "purchase",
"data": {"type": "socks"},
"anonymous_id": "anon-123",
"timestamp": 1561231234,
},
},
)
)

self.cio.track_anonymous(
"anon-123", "purchase", {"type": "socks"}, timestamp=1561231234
)

def test_pageview_call(self):
self.cio.http.hooks = dict(
Expand Down Expand Up @@ -394,7 +533,7 @@ def test_ids_are_encoded_in_url(self):
},
)
)
self.cio.track(customer_id="1 ", name="test")
self.cio.track(customer_id="1 ", name="test", data={})

self.cio.http.hooks = dict(
response=partial(
Expand Down
Loading