Skip to content

Commit 8682091

Browse files
authored
Merge pull request #8 from PostHog/add-tests
Fix tests and clean up integration
2 parents c944d7d + 8977c4e commit 8682091

File tree

10 files changed

+120
-257
lines changed

10 files changed

+120
-257
lines changed

.github/workflows/tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Backend CI
2+
3+
on:
4+
- pull_request
5+
6+
jobs:
7+
tests:
8+
name: Python tests
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v1
13+
with:
14+
fetch-depth: 1
15+
16+
- name: Set up Python 3.7
17+
uses: actions/setup-python@v1
18+
with:
19+
python-version: 3.7
20+
21+
- name: Install requirements.txt dependencies with pip
22+
run: |
23+
python -m pip install -e .
24+
25+
- name: Run posthog tests
26+
run: |
27+
python setup.py test

example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
# Where you host PostHog, with no trailing /.
1010
# You can remove this line if you're using posthog.com
11-
posthog.host = 'http://127.0.0.1:8000'
11+
# posthog.host = 'http://127.0.0.1:8000'
1212

1313
# Capture an event
1414
posthog.capture('distinct_id', 'event', {'property1': 'value', 'property2': 'value'})
1515

16-
# Alias a previous distinct id with a new one
17-
posthog.alias('distinct_id', 'new_distinct_id')
16+
# # Alias a previous distinct id with a new one
17+
# posthog.alias('distinct_id', 'new_distinct_id')
1818

19-
# Add properties to the person
20-
posthog.identify('distinct_id', {'email': '[email protected]'})
19+
# # Add properties to the person
20+
# posthog.identify('distinct_id', {'email': '[email protected]'})

posthog/client.py

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ def identify(self, distinct_id=None, properties=None, context=None, timestamp=No
7878
msg = {
7979
'timestamp': timestamp,
8080
'context': context,
81-
'type': 'identify',
8281
'distinct_id': distinct_id,
8382
'$set': properties,
8483
'event': '$identify',
@@ -100,7 +99,6 @@ def capture(self, distinct_id=None, event=None, properties=None, context=None,
10099
'timestamp': timestamp,
101100
'context': context,
102101
'distinct_id': distinct_id,
103-
'type': 'capture',
104102
'event': event,
105103
'messageId': message_id,
106104
}
@@ -121,81 +119,28 @@ def alias(self, previous_id=None, distinct_id=None, context=None,
121119
},
122120
'timestamp': timestamp,
123121
'context': context,
124-
'type': 'alias',
125122
'event': '$create_alias'
126123
}
127124

128125
return self._enqueue(msg)
129126

130-
def group(self, distinct_id=None, group_id=None, traits=None, context=None,
131-
timestamp=None, message_id=None):
132-
traits = traits or {}
133-
context = context or {}
134-
135-
require('distinct_id', distinct_id, ID_TYPES)
136-
require('group_id', group_id, ID_TYPES)
137-
require('traits', traits, dict)
138-
139-
msg = {
140-
'timestamp': timestamp,
141-
'groupId': group_id,
142-
'context': context,
143-
'distinct_id': distinct_id,
144-
'traits': traits,
145-
'type': 'group',
146-
'messageId': message_id,
147-
}
148-
149-
return self._enqueue(msg)
150-
151-
def page(self, distinct_id=None, category=None, name=None, properties=None,
127+
def page(self, distinct_id=None, url=None, properties=None,
152128
context=None, timestamp=None, message_id=None):
153129
properties = properties or {}
154130
context = context or {}
155131

156132
require('distinct_id', distinct_id, ID_TYPES)
157133
require('properties', properties, dict)
158134

159-
if name:
160-
require('name', name, string_types)
161-
if category:
162-
require('category', category, string_types)
163-
164-
msg = {
165-
'properties': properties,
166-
'timestamp': timestamp,
167-
'category': category,
168-
'context': context,
169-
'distinct_id': distinct_id,
170-
'type': 'page',
171-
'name': name,
172-
'messageId': message_id,
173-
}
174-
175-
return self._enqueue(msg)
176-
177-
def screen(self, distinct_id=None, category=None, name=None, properties=None,
178-
context=None, timestamp=None, message_id=None):
179-
properties = properties or {}
180-
context = context or {}
181-
182-
require('distinct_id', distinct_id, ID_TYPES)
183-
require('properties', properties, dict)
184-
185-
if name:
186-
require('name', name, string_types)
187-
if category:
188-
require('category', category, string_types)
135+
require('url', url, string_types)
136+
properties['$current_url'] = url
189137

190138
msg = {
191-
139+
'event': '$pageview',
192140
'properties': properties,
193141
'timestamp': timestamp,
194-
'category': category,
195142
'context': context,
196143
'distinct_id': distinct_id,
197-
'type': 'screen',
198-
'name': name,
199144
'messageId': message_id,
200145
}
201146

@@ -210,7 +155,6 @@ def _enqueue(self, msg):
210155
if message_id is None:
211156
message_id = uuid4()
212157

213-
require('type', msg['type'], string_types)
214158
require('timestamp', timestamp, datetime)
215159
require('context', msg['context'], dict)
216160

@@ -233,15 +177,15 @@ def _enqueue(self, msg):
233177
return True, msg
234178

235179
if self.sync_mode:
236-
self.log.debug('enqueued with blocking %s.', msg['type'])
180+
self.log.debug('enqueued with blocking %s.', msg['event'])
237181
post(self.api_key, self.host, gzip=self.gzip,
238182
timeout=self.timeout, batch=[msg])
239183

240184
return True, msg
241185

242186
try:
243187
self.queue.put(msg, block=False)
244-
self.log.debug('enqueued %s.', msg['type'])
188+
self.log.debug('enqueued %s.', msg['event'])
245189
return True, msg
246190
except queue.Full:
247191
self.log.warning('analytics-python queue is full')

posthog/test/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
def all_names():
88
for _, modname, _ in pkgutil.iter_modules(__path__):
9-
yield 'analytics.test.' + modname
9+
yield 'posthog.test.' + modname
1010

1111

1212
def all():

0 commit comments

Comments
 (0)