Coder Social home page Coder Social logo

castle-python's Introduction

Castle.io Python Library

So the tricky thing about this is the request metadata, which is totally necessary for good tracking, has to be fed in manually on initialization. There's a demo in the little flask app showing how to handle it with Flask's awesome reqeust module, but the general idea is:

import json
from flask import request
from castle.castle import Castle

# assume the stuff below is happening in a context that has access to a request
# you can see the real working example in ./flask_demo_server.py
api_secret = '<YOUR_API_SECRET>'
request_metadata = {
    'X-Castle-Ip': request.remote_addr,
    'X-Castle-Cookie-Id': request.cookies.get('__cid'),
    'X-Castle-Headers': json.dumps({
        'Accept': request.headers.get('ACCEPT'),
        'Accept-Encoding': request.headers.get('ACCEPT_ENCODING'),
        'Accept-Language': request.headers.get('ACCEPT_LANGUAGE'),
        'User-Agent': request.headers.get('USER_AGENT'),
    })
}

castle = Castle(api_secret=api_secret, request_metadata=request_metadata)
castle.track(name='$login.succeeded', user_id='<PUT_A_USER_ID_HERE>')

To verify, add your app_id, api_secret, and a fake user_id in the appropriate places in flask_demo_server.py. Start it up with python flask_demo_server.py and visit localhost:8181. Refresh the page so Castle has a chance to recognize the cookie, and verify through Castle's dashboard that a user with your fake id has been tracked.

License: MIT

castle-python's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.