Skip to content

Commit 1b411b3

Browse files
Merge pull request #7934 from segmentio/DOC-1191
Analytics Python review
2 parents 3fd5f63 + 739b76f commit 1b411b3

File tree

1 file changed

+28
-37
lines changed
  • src/connections/sources/catalog/libraries/server/python

1 file changed

+28
-37
lines changed

src/connections/sources/catalog/libraries/server/python/index.md

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ title: Analytics for Python
33
id: XRksQPCr7X
44
support_type: maintenance
55
---
6-
Segment's Python library lets you record analytics data from your Python code. The requests hit Segment's servers, and then Segment routes your data to any analytics service you enable on your destinations page.
6+
Segment's Python library lets you record analytics data from your Python code. The requests hit Segment's servers and then Segment routes your data to any analytics service you enable on your destinations page.
77

8-
This library is open-source, so you can [check it out on GitHub](https://github.com/segmentio/analytics-python).
8+
This library is open-source. [Check it out on GitHub](https://github.com/segmentio/analytics-python){:target="_blank”}.
99

1010
All of Segment's server-side libraries are built for high-performance, so you can use them in your web server controller code. This library uses an internal queue to make Identify and Track calls non-blocking and fast. It also batches messages and flushes asynchronously to Segment's servers using a separate thread.
1111

12-
Want to stay updated on releases? Subscribe to the [release feed](https://github.com/segmentio/analytics-python/releases.atom).
12+
Want to stay updated on releases? Subscribe to the [release feed on GitHub](https://github.com/segmentio/analytics-python/releases.atom){:target="_blank”}.
1313

14-
## Getting Started
14+
## Getting started
1515

1616
Install `segment-analytics-python` using pip:
1717

1818
```
1919
pip install segment-analytics-python
2020
```
2121

22-
If you're using a system for managing dependencies, you'll want to pin the library to `1.X` to avoid breaking changes when the library is updated
22+
If you're using a system for managing dependencies, you'll want to pin the library to `1.X` to avoid breaking changes when the library is updated.
2323

2424
Inside your app, you'll want to **set your `write_key`** before making any analytics calls:
2525

@@ -66,9 +66,9 @@ analytics.send = False
6666
6767
The Identify method lets you tie a user to their actions and record traits about them. It includes a unique User ID and any optional traits you know about them.
6868

69-
Segment recommends that you call Identify once when the user's account is created, and later when their traits change.
69+
Segment recommends that you call Identify once when the user's account is created, and later when their traits change.
7070

71-
Example Identify call:
71+
The following example Identify call is identifying John by his unique User ID (the one you know him by in your database) and labeling him with `email`, `name` and `friends` traits.
7272

7373
```python
7474
analytics.identify('019mr8mf4r', {
@@ -78,9 +78,7 @@ analytics.identify('019mr8mf4r', {
7878
})
7979
```
8080

81-
The example Identify call is identifying John by his unique User ID (the one you know him by in your database) and labeling him with `email`, `name` and `friends` traits.
82-
83-
The Identify call has the following fields:
81+
An Identify call has the following fields:
8482

8583
| Field | Description |
8684
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
@@ -92,7 +90,7 @@ The Identify call has the following fields:
9290
| `integrations` _dict, optional_ | A dictionary of destinations to enable or disable |
9391

9492

95-
Find details on the **Identify method payload** in the [Segment Spec](/docs/connections/spec/identify/).
93+
Find details on the **Identify method payload** in the [Segment Spec](/docs/connections/spec/identify/).
9694

9795
## Track
9896

@@ -102,16 +100,15 @@ You'll want to track events that are indicators of success for your site, like *
102100

103101
To get started, Segment recommends tracking just a few important events. You can always add more later.
104102

105-
Example Track call:
103+
The following sample Track call informs Segment that your user just triggered the **Signed Up** event and chose your hypothetical `'Enterprise'` plan.
106104

107105
```python
108106
analytics.track('f4ca124298', 'Signed Up', {
109107
'plan': 'Enterprise'
110108
})
111109
```
112-
This call informs Segment that your user just triggered the **Signed Up** event and chose your hypothetical `'Enterprise'` plan.
113110

114-
Track event properties can be anything you want to record, for example:
111+
Track event properties can be anything you want to record. For example, here's an Track event for a user who bookmarked an article:
115112

116113
```python
117114
analytics.track('f4ca124298', 'Article Bookmarked', {
@@ -121,7 +118,7 @@ analytics.track('f4ca124298', 'Article Bookmarked', {
121118
})
122119
```
123120

124-
The Track method has the following fields:
121+
A Track call has the following fields:
125122

126123
| Field | Description |
127124
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -142,14 +139,14 @@ The [Page](/docs/connections/spec/page) method lets you record page views on you
142139

143140
If you use a client-side set up in combination with the Python library, page calls are **already tracked for you** by default. If you want to record your own page views manually and aren't using a client-side library, read on.
144141

145-
Example Page call:
142+
As an example, here's a sample event for users who view this page:
146143

147144
```python
148145
analytics.page('user_id', 'Docs', 'Python', {
149146
'url': 'http://segment.com'
150147
})
151148
```
152-
The Page call has the following fields:
149+
A Page call has the following fields:
153150

154151
| Field | Description |
155152
| ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -162,7 +159,6 @@ The Page call has the following fields:
162159
| `anonymous_id` _string or int, optional_ | An anonymous session ID for this user. |
163160
| `integrations` _dict, optional_ | A dictionary of destinations to enable or disable |
164161

165-
166162
Find details on the **Page method payload** in the [Segment Spec](/docs/connections/spec/page/).
167163

168164
## Screen
@@ -171,7 +167,7 @@ The [Screen](/docs/connections/spec/screen) method lets you record screen views
171167

172168
If you use a Segment mobile SDK in combination with the library, screen calls are **already tracked for you** by default.If you want to record your own screen views manually and don't use a Segment SDK library, learn how below.
173169

174-
Example Screen call:
170+
Here's an example of a Screen call for the user settings page in your app:
175171

176172
```python
177173
analytics.screen('user_id', 'Settings', 'Brightness', {
@@ -192,17 +188,15 @@ The Screen call has the following fields:
192188
| `anonymous_id` _string or int, optional_ | An anonymous session ID for this user. |
193189
| `integrations` _dict, optional_ | A dictionary of destinations to enable or disable |
194190

195-
196191
Find details on the **Screen method payload** in the [Segment Spec](/docs/connections/spec/screen/).
197192

198-
---
199193
## Group
200194

201195
Group lets you associate an [identified user](/docs/connections/sources/catalog/libraries/server/python/#identify) with a group. A group could be a company, organization, account, project or team. It also lets you record custom traits about the group, like industry or number of employees.
202196

203197
This is useful for tools like [Intercom](/docs/connections/destinations/catalog/intercom/), [Preact](/docs/connections/destinations/catalog/preact/) and [Totango](/docs/connections/destinations/catalog/totango/), as it ties the user to a **group** of other users.
204198

205-
Example Group call:
199+
Here's an example Group call linking your user to their employer, Initech:
206200

207201
```python
208202
analytics.group('user_id', 'group_id', {
@@ -211,7 +205,7 @@ analytics.group('user_id', 'group_id', {
211205
})
212206
```
213207

214-
The Group call has the following fields:
208+
A Group call has the following fields:
215209

216210
| Field | Description |
217211
| ---------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -223,16 +217,15 @@ The Group call has the following fields:
223217
| `anonymous_id` _string or int, optional_ | An anonymous session ID for this user. |
224218
| `integrations` _dict, optional_ | A dictionary of destinations to enable or disable |
225219

226-
227-
Find more details about Group, including the **Group method payload**, in the [Segment Spec](/docs/connections/spec/group/).
220+
Find more details about Group calls, including the **Group method payload**, in the [Segment Spec](/docs/connections/spec/group/).
228221

229222
## Alias
230223

231224
Alias is how you associate one identity with another. This is an advanced method, but it is required to manage user identities successfully in *some* of Segment's destinations.
232225

233-
In [Mixpanel](/docs/connections/destinations/catalog/mixpanel/#alias) it's used to associate an anonymous user with an identified user once they sign up. For [Kissmetrics](/docs/connections/destinations/catalog/kissmetrics/#alias) if your user switches IDs, you can use Alias to rename the 'userId'.
226+
In [Mixpanel](/docs/connections/destinations/catalog/mixpanel/#alias) it's used to associate an anonymous user with an identified user once they sign up. For [Kissmetrics](/docs/connections/destinations/catalog/kissmetrics/#alias) if your user switches IDs, you can use Alias to rename the `userId`.
234227

235-
Example Alias call:
228+
Here's an example of an Alias call that associates a previous user ID with a new ID:
236229

237230
```python
238231
analytics.alias(previous_id, user_id)
@@ -271,7 +264,7 @@ For more details about Alias, including the **Alias call payload**, see the [Seg
271264

272265
You can import historical data by adding the `timestamp` argument to any of your method calls. This can be helpful if you've just switched to Segment.
273266

274-
Historical imports can only be done into destinations that can accept historical timestamped data. Most analytics tools like Mixpanel, Amplitude, Kissmetrics, etc. can handle that type of data just fine. One common destination that does not accept historical data is Google Analytics since their API cannot accept historical data.
267+
Historical imports can only be done into destinations that can accept historical timestamped data. Most analytics tools like Mixpanel, Amplitude, or Kissmetrics, for example, can handle that type of data just fine. One common destination that does not accept historical data is Google Analytics, since their API cannot accept historical data.
275268

276269
> info ""
277270
> If you track things that are happening right now, omit the `timestamp` and Segment's servers will timestamp the requests for you.
@@ -365,8 +358,6 @@ for entry in log:
365358
analytics.flush()
366359
```
367360

368-
---
369-
370361
## Selecting destinations
371362

372363
The Alias, Group, Identify, Page, and Track calls can all be passed an object of `integrations` that lets you turn certain destinations on or off. By default all destinations are enabled.
@@ -382,13 +373,13 @@ analytics.track('9742', 'Song Played', integrations={
382373
```
383374
This example illustrates that this track call goes only to Kissmetrics. `'all': False` says that no destination should be enabled unless otherwise specified. `'Kissmetrics': True` enables Kissmetrics.
384375

385-
Destination flags are **case sensitive** and match [the destination's name in the docs](/docs/connections/destinations/) (for example, "AdLearn Open Platform", "awe.sm", or "MailChimp").
376+
Destination flags are **case sensitive** and match [the destination's name in the docs](/docs/connections/destinations/), for example, "AdLearn Open Platform", "awe.sm", or "MailChimp".
386377

387378
## Batching
388379

389380
Segment's libraries are built to support high performance environments. It's safe to use analytics-python on a web server that serves hundreds of requests per second.
390381

391-
Every method you call **does not** result in an HTTP request, but is queued in memory instead. Messages are flushed in batch in the background, which allows for much faster operation.
382+
Every method you call **doesn't** result in an HTTP request, but is queued in memory instead. Messages are flushed in batch in the background, which allows for much faster operation.
392383

393384
By default, Segment's Python library will flush:
394385

@@ -445,7 +436,7 @@ logging.getLogger('segment').setLevel('DEBUG')
445436

446437
## Options
447438

448-
If you hate defaults or want to send data to multiple sources, then you can create your own Clients. Keep in mind that each client runs a separate background thread, so you won't want to create these on every request. Check out these gizmos:
439+
If you hate defaults or want to send data to multiple sources, then you can create your own Clients. Keep in mind that each client runs a separate background thread, so you won't want to create these on every request. Check out this sample code:
449440

450441
```python
451442
from analytics import Client
@@ -458,7 +449,7 @@ Client('YOUR_WRITE_KEY', debug=True, on_error=on_error, send=True,
458449
| `debug` _bool_ | `True` to log more verbosely, `False` by default. |
459450
| `send` _bool_ | `False` to avoid sending data to Segment, `True` by default. |
460451
| `on_error` _function_ | Set an error handler to be called whenever errors occur |
461-
| `max_queue_size` _int_ | Maximum number of elements allowed in the queue. If this condition is ever reached, that means you're identifying / tracking faster than you can flush. If this happens, [let Segment know](https://segment.com/help/contact/). |
452+
| `max_queue_size` _int_ | Maximum number of elements allowed in the queue. If this condition is ever reached, that means you're identifying or tracking faster than you can flush. If this happens, [let Segment know](https://segment.com/help/contact/). |
462453
| `upload_interval` _float_ | The frequency, in seconds, to send data to Segment. Default value is 0.5. |
463454
| `upload_size` _int_ | Number of items in a batch to upload. Default value is 100. |
464455
| `gzip` _bool_ | `True` to compress data with gzip before sending, `False` by default. |
@@ -490,7 +481,7 @@ default_app_config = 'myapp.apps.MyAppConfig'
490481

491482
### How do I add logging to Django?
492483

493-
If you're troubleshooting your analytics, you'll want to turn on logging.
484+
If you're troubleshooting your Analytics setup, you'll want to turn on logging.
494485

495486
You need to add the `analytics` logger and handler to your `settings.py`.
496487

@@ -535,7 +526,7 @@ Google App Engine may not resolve project dependencies. If this is the case add
535526
- [requests](https://github.com/kennethreitz/requests){:target="_blank"}
536527
- [python-dateutil](https://github.com/paxan/python-dateutil){:target="_blank"}
537528

538-
If you're having issues with threads outliving your request, check [Background threads and synchronous mode](#background-threads-and-synchronous-mode)
529+
If you're having issues with threads outliving your request, check the [Background threads and synchronous mode](#background-threads-and-synchronous-mode) docs.
539530

540531
## OAuth 2.0
541532

@@ -603,7 +594,7 @@ Check that you have the most recent version.
603594
python -c "import segment.analytics as analytics; print analytics.VERSION"
604595
```
605596

606-
Does it match [the most current version](https://github.com/segmentio/analytics-python/blob/master/analytics/version.py#L2)?
597+
Does it match [the most current version](https://github.com/segmentio/analytics-python/blob/master/analytics/version.py#L2){:target="_blank”}?
607598

608599
If not, update your version.
609600

0 commit comments

Comments
 (0)