Compare commits

..
Author SHA1 Message Date
James HawkinsandGitHub df0dc10f36 Update README.md 2020-02-18 18:20:20 -08:00
Tim Glaser 34e92615c2 License 2020-02-18 17:15:29 -08:00
Tim Glaser cf09644d4f Clarify alias call frontend vs backend 2020-02-18 17:13:58 -08:00
Tim Glaser 0cfce57d0e Fix error in documentation 2020-02-18 17:12:14 -08:00
Tim Glaser e9862cf671 Fix properties undefined error 2020-02-18 15:44:56 -08:00
Tim Glaser 0ce011a36d Correctly send in properties 2020-02-17 19:20:57 -08:00
5 changed files with 11 additions and 122 deletions
+2
View File
@@ -1,5 +1,7 @@
Copyright (c) 2020 PostHog (part of Hiberly Inc)
Copyright (c) 2013 Segment Inc. friends@segment.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
+2 -115
View File
@@ -1,118 +1,5 @@
# PostHog Python
Official PostHog Python library to capture and send events to any PostHog instance (including PostHog.com).
Please see the main [PostHog docs](https://github.com/PostHog/posthog/wiki).
This library uses an internal queue to make calls non-blocking and fast. It also batches requests and flushes asynchronously, making it perfect to use in any part of your web app or other server side application that needs performance.
## Installation
```bash
pip install posthog
```
In your app, import the posthog library and set your api key **before** making any calls.
```python
import posthog
posthog.api_key = 'YOUR API KEY'
```
You can find your key in the /setup page in PostHog.
To debug, you can set debug mode.
```python
posthog.debug = True
```
## Making calls
### Capture
Capture allows you to capture anything a user does within your system, which you can later use in PostHog to find patterns in usage, work out which features to improve or where people are giving up.
A `capture` call requires
- `distinct id` which uniquely identifies your user
- `event name` to make sure
- We recommend using [verb] [noun], like `movie played` or `movie updated` to easily identify what your events mean later on.
Optionally you can submit
- `properties`, which can be a dict with any information you'd like to add
For example:
```python
posthog.capture('distinct id', 'movie played', {'movie_id': '123', 'category': 'romcom'})
```
### Identify
Identify lets you add metadata on your users so you can more easily identify who they are in PostHog, and even do things like segment users by these properties.
An `identify` call requires
- `distinct id` which uniquely identifies your user
- `properties` with a dict with any key: value pairs
For example:
```python
posthog.capture('distinct id', {
'email': 'dwayne@gmail.com',
'name': 'Dwayne Johnson'
})
```
The most obvious place to make this call is whenever a user signs up, or when they update their information.
### Alias
To marry up whatever a user does before they sign up or log in with what they do after you need to make an alias call. This will allow you to answer questions like "Which marketing channels leads to users churning after a month?" or "What do users do on our website before signing up?"
In a purely back-end implementation, this means whenever an anonymous user does something, you'll want to send a session ID ([Django](https://stackoverflow.com/questions/526179/in-django-how-can-i-find-out-the-request-session-sessionid-and-use-it-as-a-vari), [Flask](https://stackoverflow.com/questions/15156132/flask-login-how-to-get-session-id)) with the capture call. Then, when that users signs up, you want to do an alias call with the session ID and the newly created user ID.
The same concept applies for when a user logs in.
An `alias` call requires
- `previous distinct id` the unique ID of the user before
- `distinct id` the current unique id
For example:
```python
posthog.alias('anonymous session id', 'distinct id')
```
## Django
For Django, you can do the initialisation of the key in the AppConfig, so that it's available everywhere.
in `yourapp/apps.py`
```python
from django.apps import AppConfig
import posthog
class YourAppConfig(AppConfig):
def ready(self):
posthog.api_key = 'your key'
```
Then, anywhere else in your app you can do
```python
import posthog
def homepage(request):
# example capture
posthog.capture(request.session.session_key, 'page view', ....)
```
# Development
## Naming confusion
As our open source project [PostHog](https://github.com/PostHog/posthog) shares the same module name, we create a special `posthog-analytics` package, mostly for internal use to avoid module collision. It is the exact same.
## How to release
1. Increase `VERSION` in `posthog/version.py`
2. run `make release` and `make release_analytics`
3. `git commit -am "Release X.Y.Z."` (where X.Y.Z is the new version)
4. `git tag -a X.Y.Z -m "Version X.Y.Z"` (where X.Y.Z is the new version).
## Thank you
This library is largely based on the `analytics-python` package.
Specifically, the Python integration details are [here](https://github.com/PostHog/posthog/wiki/Python-integration).
+4 -2
View File
@@ -218,8 +218,10 @@ class Client(object):
timestamp = guess_timezone(timestamp)
msg['timestamp'] = timestamp.isoformat()
msg['messageId'] = stringify_id(message_id)
msg['library'] = 'posthog-python'
msg['library_version'] = VERSION
if not msg.get('properties'):
msg['properties'] = {}
msg['properties']['$lib'] = 'posthog-python'
msg['properties']['$lib_version'] = VERSION
msg['distinct_id'] = stringify_id(msg.get('distinct_id', None))
+2 -4
View File
@@ -63,10 +63,8 @@ class TestClient(unittest.TestCase):
self.assertEqual(msg['properties'], {'property': 'value'})
self.assertEqual(msg['context']['ip'], '192.168.0.1')
self.assertEqual(msg['event'], 'python test event')
self.assertEqual(msg['context']['library'], {
'name': 'analytics-python',
'version': VERSION
})
self.assertEqual(msg['properties']['$lib'], 'posthog-python')
self.assertEqual(msg['properties']['$lib_version'], VERSION)
self.assertEqual(msg['messageId'], 'messageId')
self.assertEqual(msg['distinct_id'], 'distinct_id')
self.assertEqual(msg['type'], 'track')
+1 -1
View File
@@ -1 +1 @@
VERSION = '1.0.6'
VERSION = '1.0.8'