跳转到主要内容

Segment.io为Pyramid Web框架提供的包装器

项目描述

Segment.io初始化器,用于Pyramid Web框架。100%由Nose测试覆盖。

配置

  • 要使用pyramid_analytics,您应该在配置文件中包含以下指令

    pyramid.includes =
        pyramid_analytics
  • 以下配置选项可用。注意,analytics.api_tokenanalytics.secret是必需的。

    analytics.api_token = mytoken
    analytics.async = True
    analytics.flush_after = 10
    analytics.flush_at = 20
    analytics.max_queue_size = 100000
    analytics.secret = mysekret
    analytics.send = True
  • flush_after指定在服务器刷新之前没有刷新的时间(以秒为单位)。与flush_at大小策略一起使用。

  • 要管理由Segment.io的analytics-python指定的log_levellog选项,您应该在您的.ini文件中设置以下配置

    [logger_analytics]
    level = WARN
    handlers = console  # The config needs to include this handler
    qualname = analytics
    propagate = 0  # This "disables" logging
    formatter = generic  # The config needs to include this formatter

    此外,您还需要注册此记录器

  • 在底层,analytics-python通过将日志级别设置为CRITICAL来关闭日志记录。设置propagate = 0可能是一个更优雅的解决方案。

  • 所有其他选项的详细信息请参阅Segment.io的Python文档

Pyramid集成

  • 此库将一个属性analytics附加到请求对象上。pyramid_analytics会检查Pyramid api中的authenticated_userid,并为认证用户识别或跟踪数据。

  • 要更新此analytics包代表其发起调用的用户ID,您应该发送一个新的UpdatedAnalyticsUserId事件。示例

    from pyramid_analytics.events import UpdatedAnalyticsUserId
    
    # ...
    # User logged in
    event = UpdatedAnalyticsUserId(request, userid, alias=False)
    request.registry.notify(event)

    此事件将更新分析数据。如果用户注册了您的服务,您应设置alias=True。这不会向segment.io发送任何代码,而是与下面描述的Jinja2模板良好地协作。

  • 此分析包尽可能地直接从您的服务器发送数据,并回退到通过Jinja2模板向浏览器提供事件。以下是一些标识和跟踪的示例

    # identify the authenticated user with certain traits (timestamp and
    # context are optional and described by the segment.io documentation)
    request.analytics.identify(traits, timestamp=None, context=None)
    
    # identify a (not identified) user with certain traits (timestamp and
    # context are optional and described by the segment.io documentation)
    request.analytics.identify_foreign(user_id, traits, timestamp=None,
                                       context=None)
    
    # track an authenticated user with an event and properties (timestamp and
    # context are optional and described by the segment.io documentation)
    request.analytics.track(event, properties, timestamp=None, context=None)
    
    # track a (not identified) user with an event and properties (timestamp and
    # context are optional and described by the segment.io documentation)
    request.analytics.track_foreign(user_id, event, properties, timestamp=None,
                                    context=None)
  • 当Pyramid触发BeforeRender事件时,pyramid_analytics会将以下内容添加到渲染器的全局变量中

    analytics: {
       api_token: 'mytoken',
       tracking: {
            alias: userid, // or null if analytics hasn't been told to alias
            events: [{event: 'myevent',
                        properties: {property1: 'pvalue1'},
                        context: {context1: 'cvalue1'}}]
            identify: userid, // or null
        }
        tracking_json: '...' // the tracking item as json
    }

Jinja2集成

  • 提供了一个易于使用的Jinja2模板。要使用此模板,您必须将pyramid_analytics添加到您的jinja2搜索路径。如果您使用Pyramid的pyramid_jinja2,请按照以下示例更新您的config.ini文件

    jinja2.directories =
        pyramid_analytics:templates
        myapp:templates
  • 要使用模板,请在包含segment.io的javascript后将其放置在您的模板中

    {# Segment.io's javascript precedes this... #}
    {% include 'analytics.jinja2' %}

附加

0.2.1 (2013-07-03)

  • 将MANIFEST.in固定以包含所有必要的文件

0.2 (2013-07-02)

  • 向包中添加了AnalyticsHelper,提供更深入的Pyramid集成。

0.1.2 (2013-07-02)

  • 依赖于Pyramid的asbool辅助程序以实现更健壮的布尔检测。

0.1 (2013-07-01)

  • 允许以下segment.io配置选项:secretflush_atflush_afterasyncsendmax_queue_size

  • 也支持日志配置

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

pyramid_analytics-0.2.1.tar.gz (8.4 kB 查看散列)

上传时间

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面