Chris G Chris G - 1 month ago 7
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

For google analytics, assuming you know js, you can use the google analytics api read this: https://developers.google.com/analytics/solutions/articles/gdataAnalyticsCharts

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:

<html>
...html...
<h1>views: |views|</h1>
...html...
</html>

and for python do:

page=open('index.html').read()
views=open('views.txt').read()
...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.

Comments