Analytics snippets generator extension for the Flask framework.

Overview

Travis branch Coveralls branch License PyPI PyPI PyPI PyPI

Flask-Analytics

Flask Analytics is an extension for Flask which generates analytics snippets for inclusion in templates.

Installation

$ pip install Flask-Analytics

Usage

app.py

from flask import Flask, render_template
from flask_analytics import Analytics

app = Flask(__name__)
Analytics(app)

app.config['ANALYTICS']['GAUGES']['SITE_ID'] = 'XXXXXXXXXXXXX'


@app.route('/')
def index():

    return render_template('index.html')

index.html

{{ analytics }}

result

$ curl http://localhost:5000/
<script type="text/javascript">
    var _gauges = _gauges || [];
    (function() {
        var t   = document.createElement('script');
        t.type  = 'text/javascript';
        t.async = true;
        t.id    = 'gauges-tracker';
        t.setAttribute('data-site-id', 'XXXXXXXXXXXXX');
        t.src = '//secure.gaug.es/track.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(t, s);
    })();
</script>

Services

Flask-Analytics uses keys defined in app.config['ANALYTICS'] to determine which for which services analytics snippets should be generated.

Service Keys Required
Google Analytics (ga.js) ['GOOGLE_CLASSIC_ANALYTICS']['ACCOUNT']
Universal Analytics (analytics.js) ['GOOGLE_UNIVERSAL_ANALYTICS']['ACCOUNT']
Piwik ['PIWIK']['BASE_URL']
['PIWIK']['SITE_ID']
Gaug.es ['GAUGES']['SITE_ID']
Chartbeat ['CHARTBEAT']['UID']
['CHARTBEAT']['DOMAIN']
GoSquared ['GOSQUARED']['UID']

Individual services can be disabled by setting the ENABLED key for that service (e.g. ['ANALYTICS']['PIWIK']['ENABLED']). Analytics as a whole can be disabled by setting the ENABLED key at the top (e.g. ['ANALYTICS']['ENABLED']).

When a service, or analytics as a whole, is disabled, it returns an empty string, so it's safe to keep {{analytics}} in your template.

When the configuration changes, the source for the analytics code will automatically be rebuilt the next time it's called.

Tests

$ nosetests -v --with-coverage --cover-package=flask_analytics --cover-html 
test_all (test_app.TestAnalytics) ... ok
test_boostrap (test_app.TestAnalytics) ... ok
test_chartbeat (test_app.TestAnalytics) ... ok
test_disabled (test_app.TestAnalytics) ... ok
test_gauges (test_app.TestAnalytics) ... ok
test_google_classic (test_app.TestAnalytics) ... ok
test_google_universal (test_app.TestAnalytics) ... ok
test_gosquared (test_app.TestAnalytics) ... ok
test_none (test_app.TestAnalytics) ... ok
test_piwik (test_app.TestAnalytics) ... ok

Name                                                    Stmts   Miss  Cover   Missing
-------------------------------------------------------------------------------------
flask_analytics.py                                          1      0   100%   
flask_analytics/analytics.py                               60      1    98%   77
flask_analytics/providers.py                                0      0   100%   
flask_analytics/providers/base.py                           2      0   100%   
flask_analytics/providers/chartbeat.py                     13      0   100%   
flask_analytics/providers/gauges.py                        11      0   100%   
flask_analytics/providers/googleclassicanalytics.py        11      0   100%   
flask_analytics/providers/googleuniversalanalytics.py      11      0   100%   
flask_analytics/providers/gosquared.py                     11      0   100%   
flask_analytics/providers/piwik.py                         13      0   100%   
-------------------------------------------------------------------------------------
TOTAL                                                     133      1    99%   
----------------------------------------------------------------------
Ran 10 tests in 0.111s


OK

License

Flask-Analytics is dedicated to the public domain. Please read the license for more information.

You might also like...
A Flask extension that enables or disables features based on configuration.

Flask FeatureFlags This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on

An extension to add support of Plugin in Flask.

An extension to add support of Plugin in Flask.

A simple barcode and QR code generator built in Python with Flask.
A simple barcode and QR code generator built in Python with Flask.

✨ Komi - Barcode & QR Generator ✨ A simple barcode and QR code generator built in Python with Flask. 📑 Table of Contents Usage Installation Contribut

Flask-Rebar combines flask, marshmallow, and swagger for robust REST services.

Flask-Rebar Flask-Rebar combines flask, marshmallow, and swagger for robust REST services. Features Request and Response Validation - Flask-Rebar reli

Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development.
Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development.

Flask-Starter Flask-Starter is a boilerplate starter template designed to help you quickstart your Flask web application development. It has all the r

Brandnew-flask is a CLI tool used to generate a powerful and mordern flask-app that supports the production environment.

Brandnew-flask is still in the initial stage and needs to be updated and improved continuously. Everyone is welcome to maintain and improve this CLI.

Flask Project Template A full feature Flask project template.

Flask Project Template A full feature Flask project template. See also Python-Project-Template for a lean, low dependency Python app. HOW TO USE THIS

A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask
A Fast API style support for Flask. Gives you MyPy types with the flexibility of flask

Flask-Fastx Flask-Fastx is a Fast API style support for Flask. It Gives you MyPy types with the flexibility of flask. Compatibility Flask-Fastx requir

Flask-app scaffold, generate flask restful backend

Flask-app scaffold, generate flask restful backend

Comments
  • Testing in app

    Testing in app

    This is not a bug report, but a request for clarification.

    I've installed Flask-Analytics in my app using pip.

    I have add GOOGLE_ANALYTICS_ID in app.config.

    I have included {{ analytics }} in the body of my index.html template.

    I was expecting to see some interesting new text inserted into my html output, but I note no differences.

    What should I expect to see in my html output, if anything?

    opened by kphretiq 8
  • Python3 version out of date, documentation doesn't work

    Python3 version out of date, documentation doesn't work

    It's not appararent from the website that the python2 and python3 versions are completely different, even though they have the same version number. For instance, the python version is using the older out of data Google Analytics, and is using it with different keys than the python 2 version.

    opened by tedivm 4
  • Python 3 compatibility

    Python 3 compatibility

    You nearly had it.

    my_function.__code__ is backwards compatible (at least to 2.7)

    The order of the js snippets isn't important, only their content is. For whatever reason, Py3 orders them differently.

    str(expected.encode('utf8')) is needed since UTF8 and ascii sort differently.

    opened by samizdis 2
Owner
Mihir
Infrastructure and Site Reliability Engineer. Public Domain Advocate. Security enthusiast. Lover of dogs, cheesecake, and traveling.
Mihir
flask-apispec MIT flask-apispec (🥉24 · ⭐ 520) - Build and document REST APIs with Flask and apispec. MIT

flask-apispec flask-apispec is a lightweight tool for building REST APIs in Flask. flask-apispec uses webargs for request parsing, marshmallow for res

Joshua Carp 617 Dec 30, 2022
flask-reactize is a boostrap to serve any React JS application via a Python back-end, using Flask as web framework.

flask-reactize Purpose Developing a ReactJS application requires to use nodejs as back end server. What if you want to consume external APIs: how are

Julien Chomarat 4 Jan 11, 2022
MongoEngine flask extension with WTF model forms support

Flask-MongoEngine Info: MongoEngine for Flask web applications. Repository: https://github.com/MongoEngine/flask-mongoengine About Flask-MongoEngine i

MongoEngine 815 Jan 3, 2023
A caching extension for Flask

Flask-Caching Adds easy cache support to Flask. This is a fork of the Flask-Cache extension. Flask-Caching also includes the cache module from werkzeu

Peter Justin 774 Jan 2, 2023
Rate Limiting extension for Flask

Flask-Limiter Flask-Limiter provides rate limiting features to flask routes. It has support for a configurable backend for storage with current implem

Ali-Akber Saifee 922 Jan 8, 2023
SeaSurf is a Flask extension for preventing cross-site request forgery (CSRF).

Flask-SeaSurf SeaSurf is a Flask extension for preventing cross-site request forgery (CSRF). CSRF vulnerabilities have been found in large and popular

Max Countryman 183 Dec 28, 2022
A flask extension using pyexcel to read, manipulate and write data in different excel formats: csv, ods, xls, xlsx and xlsm.

Flask-Excel - Let you focus on data, instead of file formats Support the project If your company has embedded pyexcel and its components into a revenu

null 247 Dec 27, 2022
flask extension for integration with the awesome pydantic package

Flask-Pydantic Flask extension for integration of the awesome pydantic package with Flask. Installation python3 -m pip install Flask-Pydantic Basics v

null 249 Jan 6, 2023
A Flask extension that enables or disables features based on configuration.

Flask FeatureFlags This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on

Rachel Greenfield 131 Sep 26, 2022
A Flask extension that enables or disables features based on configuration.

Flask FeatureFlags This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on

Rachel Greenfield 124 Jan 22, 2021