Chris G Chris G - 10 months ago 42
Javascript Question

Displaying google analytic page views on your site?

I was thinking of displaying my page views on my static site with Google Analytics and it seems like a fair bit of work.

Instead of doing this, would it be possible to set up a Python file that keeps a counter of views and simply adds one whenever the page is loaded?

If the Google Analytics way is simple, can someone provide an example of doing it?

Answer Source

For google analytics, assuming you know js, you can use the google analytics api read this:

It seems pretty easy, but if you want to use python...

This shouldn't be extremely hard. The quick answer is that you can change from serving static files into using python. You could store the static file into say, index.html which would look like:

<h1>views: |views|</h1>

and for python do:

views=open('views.txt').read() and increment and re-write ...
page.replace('|views|', views)
print page

however, this won't work off the bat. Your webserver will return the python file without executing it.It shouldn't be too hard, though, just do a search for whatever server you're using.