You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/connections/sources/catalog/libraries/server/python/index.md
+28-37Lines changed: 28 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,23 +3,23 @@ title: Analytics for Python
3
3
id: XRksQPCr7X
4
4
support_type: maintenance
5
5
---
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.
7
7
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”}.
9
9
10
10
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.
11
11
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”}.
13
13
14
-
## Getting Started
14
+
## Getting started
15
15
16
16
Install `segment-analytics-python` using pip:
17
17
18
18
```
19
19
pip install segment-analytics-python
20
20
```
21
21
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.
23
23
24
24
Inside your app, you'll want to **set your `write_key`** before making any analytics calls:
25
25
@@ -66,9 +66,9 @@ analytics.send = False
66
66
67
67
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.
68
68
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.
70
70
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.
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.
@@ -142,14 +139,14 @@ The [Page](/docs/connections/spec/page) method lets you record page views on you
142
139
143
140
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.
144
141
145
-
Example Page call:
142
+
As an example, here's a sample event for users who view this page:
@@ -162,7 +159,6 @@ The Page call has the following fields:
162
159
|`anonymous_id`_string or int, optional_| An anonymous session ID for this user. |
163
160
|`integrations`_dict, optional_| A dictionary of destinations to enable or disable |
164
161
165
-
166
162
Find details on the **Page method payload** in the [Segment Spec](/docs/connections/spec/page/).
167
163
168
164
## Screen
@@ -171,7 +167,7 @@ The [Screen](/docs/connections/spec/screen) method lets you record screen views
171
167
172
168
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.
173
169
174
-
Example Screen call:
170
+
Here's an example of a Screen call for the user settings page in your app:
@@ -192,17 +188,15 @@ The Screen call has the following fields:
192
188
|`anonymous_id`_string or int, optional_| An anonymous session ID for this user. |
193
189
|`integrations`_dict, optional_| A dictionary of destinations to enable or disable |
194
190
195
-
196
191
Find details on the **Screen method payload** in the [Segment Spec](/docs/connections/spec/screen/).
197
192
198
-
---
199
193
## Group
200
194
201
195
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.
202
196
203
197
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.
204
198
205
-
Example Group call:
199
+
Here's an example Group call linking your user to their employer, Initech:
@@ -223,16 +217,15 @@ The Group call has the following fields:
223
217
|`anonymous_id`_string or int, optional_| An anonymous session ID for this user. |
224
218
|`integrations`_dict, optional_| A dictionary of destinations to enable or disable |
225
219
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/).
228
221
229
222
## Alias
230
223
231
224
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.
232
225
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`.
234
227
235
-
Example Alias call:
228
+
Here's an example of an Alias call that associates a previous user ID with a new ID:
236
229
237
230
```python
238
231
analytics.alias(previous_id, user_id)
@@ -271,7 +264,7 @@ For more details about Alias, including the **Alias call payload**, see the [Seg
271
264
272
265
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.
273
266
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.
275
268
276
269
> info ""
277
270
> 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:
365
358
analytics.flush()
366
359
```
367
360
368
-
---
369
-
370
361
## Selecting destinations
371
362
372
363
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.
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.
384
375
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".
386
377
387
378
## Batching
388
379
389
380
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.
390
381
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.
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:
|`debug`_bool_|`True` to log more verbosely, `False` by default. |
459
450
|`send`_bool_|`False` to avoid sending data to Segment, `True` by default. |
460
451
|`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/). |
462
453
|`upload_interval`_float_| The frequency, in seconds, to send data to Segment. Default value is 0.5. |
463
454
|`upload_size`_int_| Number of items in a batch to upload. Default value is 100. |
464
455
|`gzip`_bool_|`True` to compress data with gzip before sending, `False` by default. |
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.
539
530
540
531
## OAuth 2.0
541
532
@@ -603,7 +594,7 @@ Check that you have the most recent version.
603
594
python -c "import segment.analytics as analytics; print analytics.VERSION"
604
595
```
605
596
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”}?
0 commit comments