Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ MANIFEST
build
.eggs
*.bat
.vscode/
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
analytics-python
==============

[![CircleCI](https://circleci.com/gh/segmentio/analytics-python.svg?style=svg&circle-token=b09aadca8e15901549cf885adcb8e1eaf671a5d8)](https://circleci.com/gh/segmentio/analytics-python)
[![CircleCI](https://circleci.com/gh/North-Two-Five/analytics-python.svg?style=svg&circle-token=7f225692265ab09f1dbe3f3a672efa137b1cfced)](https://circleci.com/gh/North-Two-Five/analytics-python)

analytics-python is a python client for [Segment](https://segment.com)

Expand All @@ -10,29 +10,29 @@ analytics-python is a python client for [Segment](https://segment.com)
<p><b><i>You can't fix what you can't measure</i></b></p>
</div>

Analytics helps you measure your users, product, and business. It unlocks insights into your app's funnel, core business metrics, and whether you have product-market fit.
Analytics helps you measure your users, product, and business. It unlocks insights into your app's funnel, core business metrics, and whether you have a product-market fit.

## How to get started
## 🚀 How to get started
1. **Collect analytics data** from your app(s).
- The top 200 Segment companies collect data from 5+ source types (web, mobile, server, CRM, etc.).
2. **Send the data to analytics tools** (for example, Google Analytics, Amplitude, Mixpanel).
- Over 250+ Segment companies send data to eight categories of destinations such as analytics tools, warehouses, email marketing and remarketing systems, session recording, and more.
- Over 250+ Segment companies send data to eight categories of destinations such as analytics tools, warehouses, email marketing, and remarketing systems, session recording, and more.
3. **Explore your data** by creating metrics (for example, new signups, retention cohorts, and revenue generation).
- The best Segment companies use retention cohorts to measure product market fit. Netflix has 70% paid retention after 12 months, 30% after 7 years.
- The best Segment companies use retention cohorts to measure product-market fit. Netflix has 70% paid retention after 12 months, 30% after 7 years.

[Segment](https://segment.com) collects analytics data and allows you to send it to more than 250 apps (such as Google Analytics, Mixpanel, Optimizely, Facebook Ads, Slack, Sentry) just by flipping a switch. You only need one Segment code snippet, and you can turn integrations on and off at will, with no additional code. [Sign up with Segment today](https://app.segment.com/signup).

### Why?
### 🤔 Why?
1. **Power all your analytics apps with the same data**. Instead of writing code to integrate all of your tools individually, send data to Segment, once.

2. **Install tracking for the last time**. We're the last integration you'll ever need to write. You only need to instrument Segment once. Reduce all of your tracking code and advertising tags into a single set of API calls.

3. **Send data from anywhere**. Send Segment data from any device, and we'll transform and send it on to any tool.

4. **Query your data in SQL**. Slice, dice, and analyze your data in detail with Segment SQL. We'll transform and load your customer behavioral data directly from your apps into Amazon Redshift, Google BigQuery, or Postgres. Save weeks of engineering time by not having to invent your own data warehouse and ETL pipeline.
4. **Query your data in SQL**. Slice, dice, and analyze your data in detail with Segment SQL. We'll transform and load your customer behavioral data directly from your apps into Amazon Redshift, Google BigQuery, or Postgres. Save weeks of engineering time by not having to invent your data warehouse and ETL pipeline.

For example, you can capture data on any app:
```js
```python
analytics.track('Order Completed', { price: 99.84 })
```
Then, query the resulting data in SQL:
Expand All @@ -41,6 +41,32 @@ Analytics helps you measure your users, product, and business. It unlocks insigh
order by price desc
```

## 👨‍💻 Getting Started

Install `analytics-python` using pip:

```bash
$ pip install analytics-python
```

or you can clone this repo:
```bash
$ git clone https://github.com/segmentio/analytics-python.git

$ cd analytics-python

$ sudo python3 setup.py install
```

Now inside your app, you'll want to **set your** `write_key` before making any analytics calls:

```python
import analytics

analytics.write_key = 'YOUR_WRITE_KEY'
```
**Note** If you need to send data to multiple Segment sources, you can initialize a new Client for each `write_key`

### 🚀 Startup Program
<div align="center">
<a href="https://segment.com/startups"><img src="https://user-images.githubusercontent.com/16131737/53128952-08d3d400-351b-11e9-9730-7da35adda781.png" /></a>
Expand Down Expand Up @@ -75,3 +101,5 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)
2 changes: 1 addition & 1 deletion analytics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, write_key=None, host=None, debug=False,
# to call flush().
if send:
atexit.register(self.join)
for n in range(thread):
for _ in range(thread):
self.consumers = []
consumer = Consumer(
self.queue, write_key, host=host, on_error=on_error,
Expand Down
2 changes: 1 addition & 1 deletion analytics/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def upload(self):
self.on_error(e, batch)
finally:
# mark items as acknowledged from queue
for item in batch:
for _ in batch:
self.queue.task_done()
return success

Expand Down
6 changes: 3 additions & 3 deletions analytics/request.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from datetime import date, datetime
from dateutil.tz import tzutc
from io import BytesIO
from gzip import GzipFile
import logging
import json
from gzip import GzipFile
from dateutil.tz import tzutc
from requests.auth import HTTPBasicAuth
from requests import sessions
from io import BytesIO

from analytics.version import VERSION
from analytics.utils import remove_trailing_slash
Expand Down
24 changes: 12 additions & 12 deletions analytics/test/client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from datetime import date, datetime
import unittest
import time
import six
import mock
import time

from analytics.version import VERSION
from analytics.client import Client
Expand Down Expand Up @@ -238,8 +238,8 @@ def test_advanced_screen(self):
def test_flush(self):
client = self.client
# set up the consumer with more requests than a single batch will allow
for i in range(1000):
success, msg = client.identify('userId', {'trait': 'value'})
for _ in range(1000):
_, _ = client.identify('userId', {'trait': 'value'})
# We can't reliably assert that the queue is non-empty here; that's
# a race condition. We do our best to load it up though.
client.flush()
Expand All @@ -249,8 +249,8 @@ def test_flush(self):
def test_shutdown(self):
client = self.client
# set up the consumer with more requests than a single batch will allow
for i in range(1000):
success, msg = client.identify('userId', {'trait': 'value'})
for _ in range(1000):
_, _ = client.identify('userId', {'trait': 'value'})
client.shutdown()
# we expect two things after shutdown:
# 1. client queue is empty
Expand All @@ -262,7 +262,7 @@ def test_shutdown(self):
def test_synchronous(self):
client = Client('testsecret', sync_mode=True)

success, message = client.identify('userId')
success, _ = client.identify('userId')
self.assertFalse(client.consumers)
self.assertTrue(client.queue.empty())
self.assertTrue(success)
Expand All @@ -272,10 +272,10 @@ def test_overflow(self):
# Ensure consumer thread is no longer uploading
client.join()

for i in range(10):
for _ in range(10):
client.identify('userId')

success, msg = client.identify('userId')
success, _ = client.identify('userId')
# Make sure we are informed that the queue is at capacity
self.assertFalse(success)

Expand Down Expand Up @@ -322,7 +322,7 @@ def test_user_defined_flush_at(self):
flush_at=10, flush_interval=3)

def mock_post_fn(*args, **kwargs):
self.assertEquals(len(kwargs['batch']), 10)
self.assertEqual(len(kwargs['batch']), 10)

# the post function should be called 2 times, with a batch size of 10
# each time.
Expand All @@ -331,14 +331,14 @@ def mock_post_fn(*args, **kwargs):
for _ in range(20):
client.identify('userId', {'trait': 'value'})
time.sleep(1)
self.assertEquals(mock_post.call_count, 2)
self.assertEqual(mock_post.call_count, 2)

def test_user_defined_timeout(self):
client = Client('testsecret', timeout=10)
for consumer in client.consumers:
self.assertEquals(consumer.timeout, 10)
self.assertEqual(consumer.timeout, 10)

def test_default_timeout_15(self):
client = Client('testsecret')
for consumer in client.consumers:
self.assertEquals(consumer.timeout, 15)
self.assertEqual(consumer.timeout, 15)
5 changes: 3 additions & 2 deletions analytics/test/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def test_multiple_uploads_per_interval(self):
time.sleep(flush_interval * 1.1)
self.assertEqual(mock_post.call_count, 2)

def test_request(self):
@classmethod
def test_request(cls):
consumer = Consumer(None, 'testsecret')
track = {
'type': 'track',
Expand Down Expand Up @@ -195,4 +196,4 @@ def mock_post_fn(_, data, **kwargs):
for _ in range(0, n_msgs + 2):
q.put(track)
q.join()
self.assertEquals(mock_post.call_count, 2)
self.assertEqual(mock_post.call_count, 2)
4 changes: 2 additions & 2 deletions analytics/test/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

class TestModule(unittest.TestCase):

def failed(self):
self.failed = True
# def failed(self):
# self.failed = True

def setUp(self):
self.failed = False
Expand Down
4 changes: 2 additions & 2 deletions analytics/test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_clean_with_dates(self):
}
self.assertEqual(dict_with_dates, utils.clean(dict_with_dates))

def test_bytes(self):
@classmethod
def test_bytes(cls):
if six.PY3:
item = bytes(10)
else:
Expand All @@ -67,7 +68,6 @@ def test_bytes(self):
def test_clean_fn(self):
cleaned = utils.clean({'fn': lambda x: x, 'number': 4})
self.assertEqual(cleaned['number'], 4)
# TODO: fixme, different behavior on python 2 and 3
if 'fn' in cleaned:
self.assertEqual(cleaned['fn'], None)

Expand Down
12 changes: 6 additions & 6 deletions analytics/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from dateutil.tz import tzlocal, tzutc
from datetime import date, datetime
from decimal import Decimal
import logging
import numbers

from decimal import Decimal
from datetime import date, datetime
from dateutil.tz import tzlocal, tzutc

import six

log = logging.getLogger('segment')
Expand Down Expand Up @@ -31,9 +32,8 @@ def guess_timezone(dt):
# this was created using datetime.datetime.now()
# so we are in the local timezone
return dt.replace(tzinfo=tzlocal())
else:
# at this point, the best we can do is guess UTC
return dt.replace(tzinfo=tzutc())
# at this point, the best we can do is guess UTC
return dt.replace(tzinfo=tzutc())

return dt

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import os
import sys

Expand Down
4 changes: 2 additions & 2 deletions simulator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import analytics
import logging
import argparse
import json
import logging
import analytics

__name__ = 'simulator.py'
__version__ = '0.0.1'
Expand Down