Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Backend CI

on:
- pull_request

jobs:
tests:
name: Python tests
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1

- name: Set up Python 3.7
uses: actions/setup-python@v1
with:
python-version: 3.7

- name: Install requirements.txt dependencies with pip
run: |
python -m pip install -e .

- name: Run posthog tests
run: |
python setup.py test
10 changes: 5 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

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

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

# Alias a previous distinct id with a new one
posthog.alias('distinct_id', 'new_distinct_id')
# # Alias a previous distinct id with a new one
# posthog.alias('distinct_id', 'new_distinct_id')

# Add properties to the person
posthog.identify('distinct_id', {'email': 'something@something.com'})
# # Add properties to the person
# posthog.identify('distinct_id', {'email': 'something@something.com'})
68 changes: 6 additions & 62 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def identify(self, distinct_id=None, properties=None, context=None, timestamp=No
msg = {
'timestamp': timestamp,
'context': context,
'type': 'identify',
'distinct_id': distinct_id,
'$set': properties,
'event': '$identify',
Expand All @@ -100,7 +99,6 @@ def capture(self, distinct_id=None, event=None, properties=None, context=None,
'timestamp': timestamp,
'context': context,
'distinct_id': distinct_id,
'type': 'capture',
'event': event,
'messageId': message_id,
}
Expand All @@ -121,81 +119,28 @@ def alias(self, previous_id=None, distinct_id=None, context=None,
},
'timestamp': timestamp,
'context': context,
'type': 'alias',
'event': '$create_alias'
}

return self._enqueue(msg)

def group(self, distinct_id=None, group_id=None, traits=None, context=None,
timestamp=None, message_id=None):
traits = traits or {}
context = context or {}

require('distinct_id', distinct_id, ID_TYPES)
require('group_id', group_id, ID_TYPES)
require('traits', traits, dict)

msg = {
'timestamp': timestamp,
'groupId': group_id,
'context': context,
'distinct_id': distinct_id,
'traits': traits,
'type': 'group',
'messageId': message_id,
}

return self._enqueue(msg)

def page(self, distinct_id=None, category=None, name=None, properties=None,
def page(self, distinct_id=None, url=None, properties=None,
context=None, timestamp=None, message_id=None):
properties = properties or {}
context = context or {}

require('distinct_id', distinct_id, ID_TYPES)
require('properties', properties, dict)

if name:
require('name', name, string_types)
if category:
require('category', category, string_types)

msg = {
'properties': properties,
'timestamp': timestamp,
'category': category,
'context': context,
'distinct_id': distinct_id,
'type': 'page',
'name': name,
'messageId': message_id,
}

return self._enqueue(msg)

def screen(self, distinct_id=None, category=None, name=None, properties=None,
context=None, timestamp=None, message_id=None):
properties = properties or {}
context = context or {}

require('distinct_id', distinct_id, ID_TYPES)
require('properties', properties, dict)

if name:
require('name', name, string_types)
if category:
require('category', category, string_types)
require('url', url, string_types)
properties['$current_url'] = url

msg = {

'event': '$pageview',
'properties': properties,
'timestamp': timestamp,
'category': category,
'context': context,
'distinct_id': distinct_id,
'type': 'screen',
'name': name,
'messageId': message_id,
}

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

require('type', msg['type'], string_types)
require('timestamp', timestamp, datetime)
require('context', msg['context'], dict)

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

if self.sync_mode:
self.log.debug('enqueued with blocking %s.', msg['type'])
self.log.debug('enqueued with blocking %s.', msg['event'])
post(self.api_key, self.host, gzip=self.gzip,
timeout=self.timeout, batch=[msg])

return True, msg

try:
self.queue.put(msg, block=False)
self.log.debug('enqueued %s.', msg['type'])
self.log.debug('enqueued %s.', msg['event'])
return True, msg
except queue.Full:
self.log.warning('analytics-python queue is full')
Expand Down
2 changes: 1 addition & 1 deletion posthog/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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


def all():
Expand Down
Loading