Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: How to prevent Google Analytics JS to run in dev environment?

When signing up with google analytics there is a piece of js script that needs to be inserted in the base.html

<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'xxx', 'example.com');
      ga('send', 'pageview');

    </script>

Problem with having it always there is, that development machines would submit unnecessary data that is not from real users. I was wondering if there is a way to ask if flask is in the debug mode, and hence do not load this script in first place.

How can I check for debug mode in the template?

like image 863
Houman Avatar asked Jul 02 '13 16:07

Houman


People also ask

How do I disable Google Analytics development?

To disable Analytics programmatically, set the following window property to true : window['ga-disable-GA_MEASUREMENT_ID'] = true; Replace GA_MEASUREMENT_ID with the Analytics ID of the property that you would like to disable.

What is the difference between GTAG js and analytics js?

Unlike analytics. js, gtag. js doesn't use trackers to send data to Google Analytics. It sends data to Google Analytics properties identified by their IDs set by the config command.

Does Google Analytics require JavaScript?

Both analytics. js library and gtag. js library used by Google Analytics are JavaScript files. Without JavaScript, there will be no Google Analytics and Google Tag Manager won't be useful.

How does Google Analytics work with JavaScript?

How does Google Analytics work? Google Analytics acquires user data from each website visitor through the use of page tags. A JavaScript page tag is inserted into the code of each page. This tag runs in the web browser of each visitor, collecting data and sending it to one of Google's data collection servers.


1 Answers

If you are not using it already you should create one file called config.py and then load it into your Flask app, by doing something like this:

import config
app = flask.Flask(__name__)
app.config.from_object(config)

Then in the config.py file you can write:

# Running on App Enginge
PRODUCTION = os.environ.get('SERVER_SOFTWARE', '').startswith('Google App Engine') 
# Running Localy
DEVELOPMENT = not PRODUCTION
# You decide when is DEBUG mode, usually when running locally
DEBUG = DEVELOPMENT

And then in your template you can use these values like this:

{% if not config.DEBUG %}
  <script>
    ...
  </script>
{% endif %}

You could also exclude the admin users of your application to affect the analytics since they are most likely going to abuse them anyway. If you implemented the current_user the Flask way then your template should be change to:

{% if not current_user.admin and not config.DEBUG %}
  <script>
    ...
  </script>
{% endif %}

Among other things, all of the above (config.py, main.py, analytics.html) above are used in my gae-init project.

like image 99
Lipis Avatar answered Nov 15 '22 08:11

Lipis