Skip to main content

Opinionated Pyramid integration with Mixpanel, a user behavioural analytics platform and CRM.

Project description

Integrate your Pyramid app with Mixpanel to learn who your users are and how they are using your app.

CircleCI for pyramid_mixpanel (master branch) Test coverage (master branch) Test coverage (master branch) latest version of pyramid_mixpanel on PyPI Supported Python versions License: MIT Built by these great folks! Talk to us in #pyramid on Freenode IRC

Opinionated Mixpanel integration

The reason this package exists is to provide sane defaults when integrating with Mixpanel. Instead of chasing down event name typos and debugging why tracking does not work, you can focus on learning what is important to your users.

  • You never have typo-duplicated events in Mixpanel, because every event name comes from a dataclass, never from a string that can be miss-typed by mistake.
  • Same for properties. Like events, properties are hardcoded as dataclasses.
  • All "special" and "reserved" events and properties are already provided, no need to chase them down in various Mixpanel docs.
  • Your app never stops working if Mixpanel is down, but you still get errors in your logs so you know what is going on.
  • You never forget to call flush() on the events buffer, since pyramid_mixpanel hooks into the request life-cycle and calls flush() at the end of the request processing.
  • You defer sending events until the entire request is processed successfully, i.e. never send events like "User added a thing" if adding the thing to DB failed at a later stage in the request life-cycle.

Features

  • Builds on top of https://mixpanel.github.io/mixpanel-python/.
  • Provides a handy request.mixpanel.* helper for sending events and setting profile properties.
  • Makes sure to call .flush() at the end of request life-cycle.
  • Provides dataclasses for events and properties, to avoid typos.
  • You can roll your own Consumer, for example one that schedules a background task to send events, to increase request processing speed, since HTTP requests to Mixpanel are offloaded to a background task.
  • Provides a MixpanelQuery helper to use JQL to query Mixpanel for data. Some common queries like one for getting profiles by email are included.
  • In local development and unit testing, all messages are stored in request.mixpanel.api._consumer.mocked_messages which makes writing integration tests a breeze.
  • Automatically sets Mixpanel tracking distinct_id if request.user exists. Otherwise, you need to set it manually with request.mixpanel.distinct_id = 'foo'.

Getting started

  1. Declare pyramid_mixpanel as a dependency in your Pyramid project.

  2. Include the following lines:

    config.include("pyramid_mixpanel")
    
  3. Tell mixpanel_mixpanel how you want to use it:

    # for local development and unit testing
    # events will be stored in request.mixpanel.api._consumer.mocked_messages
    mixpanel.token = false
    
    # minimal configuration
    mixpanel.token = <TOKEN>
    
    # enable support for querying Mixpanel data
    mixpanel.api_secret = <SECRET>
    
    # custom events and properties
    mixpanel.events = myapp.mixpanel.Events
    mixpanel.event_properties = myapp.mixpanel.EventProperties
    mixpanel.profile_properties = myapp.mixpanel.ProfileProperties
    
    # defer sending of Mixpanel messages to a background task queue
    mixpanel.consumer = myapp.mixpanel.QueuedConsumer
    

For view code dealing with requests, a pre-configured request.mixpanel is available.

Design defense

The authors of pyramid_openapi3 believe that while Mixpanel allows sending schema-less data, that can change as requirements for the project change, it is better to be precise about what "events" you send and what the properties of those events will be called. Same for "profiles". Here are the reasons that accumulated over 5 years of using Mixpanel at Niteo:

a) There will be typos in event and property names. They will clutter your Mixpanel dashboard and slow you down.

b) There will be differently named events for similar actions sent from different parts of your codebase. Then in your Mixpanel dashboard you'll have User Clicked Button and Button Clicked events in you won't be sure which to use, and what's the difference between them.

c) Your events and properties will not be consistently named, because they will be sent from different parts of your codebase, by different authors. Your Mixpanel dashboard will feel somewhat "broken" because some events will be in past tense (User Logged In), some in all lowers caps (generated invoice), some with only the action verb (click) and so on.

All issues outlined above are alleviated using this package because all event & property names are defined as dataclasses, in a single source of truth manner. No typos are possible once the initial specification is done. You immediately recognize bad naming patterns because all event & property names are in a single file.

Naming best practice

In order to have nice and consistent event and property names, the authors of this package suggest using the following guidelines when coming up with names:

  • Use the <item> <action> format in past tense, i.e. Button Clicked, Page Viewed, File Downloaded.
  • Use Title Case.
  • Frontend only sends two Mixpanel events: Button/Link Clicked and Page Viewed. We then construct custom events such as Password Reset Button Clicked or Pricing Page Viewed inside Mixpanel dashboard based on button name, URL, etc. Custom events can be modified retroactively, regular events cannot.
  • Backend sends "action" events, when those actions finish successfully, such as Site Deployed, PDF generated, Backup Completed.

Running tests

You need to have pipenv and Python 3.7 installed on your machine. Then you can run:

$ make tests

Related packages

These packages are in the same problem-space:

Use in the wild

A couple of projects that use pyramid_mixpanel in production:

  • WooCart - Managed WooCommerce service.
  • EasyBlogNetworks - PBN hosting and autopilot maintenance.
  • Kafkai - AI generated content.
  • Docsy - Faceted search for private projects and teams.

Changelog

0.4.2 (2019-08-24)

  • Include py.typed in the package, now for real. [zupo]

0.4.1 (2019-08-24)

  • Include py.typed in the package. [zupo]

0.4.0 (2019-08-19)

  • Prepare for PYPI release of the rewrite. [zupo]

  • Small performance optimization. [zupo]

0.3.0 (2019-08-09)

  • Add guards that make sure parameters sent to MixpanelTrack are valid. [zupo]

  • Don't flood logs with "mixpanel configured" messages. [zupo]

  • Support for the people_append method. [suryasr007]

  • Lots of cleanup of legacy assumptions:

    • profile_sync method was removed
    • request.user no longer required
    • MixpanelTrack init now accepts distinct_id instead of user
    • state ProfileProperty no longer required [zupo]

0.2.1 (2019-07-28)

  • Not all consumers have a .flush() method. [zupo]

0.2.0 (2019-07-27)

  • Rewrite based on 5 years of Mixpanel usage in production at Niteo. [@zupo, @vanclevstik, @dz0ny, @karantan, @am-on, @rokcarl]

0.1.14 - 0.1.65 (2012-2014)

  • Legacy version developed by @hadrien.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyramid_mixpanel-0.4.2.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyramid_mixpanel-0.4.2-py2.py3-none-any.whl (18.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyramid_mixpanel-0.4.2.tar.gz.

File metadata

  • Download URL: pyramid_mixpanel-0.4.2.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for pyramid_mixpanel-0.4.2.tar.gz
Algorithm Hash digest
SHA256 6fb62b99281beadb0d6fbfd316fc2c7ecb009cc7379e0e166caf46b6a52dc7d6
MD5 a7ba1ab8e8d8c2b0eccef22d6523989f
BLAKE2b-256 5ba538edbdbc201d61aab9ba883589ecc0982a018d3890b19ae06d12e6a91d1c

See more details on using hashes here.

File details

Details for the file pyramid_mixpanel-0.4.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pyramid_mixpanel-0.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for pyramid_mixpanel-0.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e034ea918f4be1d062c138bdab9876edbd50ab2f2f08cc9bbb528b02696b90ad
MD5 52a7c1cec37b7a35afc9273770d21a04
BLAKE2b-256 8341ae4f6865699947abd50148b06bf3fd2bff4d995c4e837f1ca935d3c905ee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page