Count page views

Is there a way to count how many people viewed the streamlit app, or gather other interesting statistics in viewers (time, ip address…).

Hi @gretzteam, welcome to the Streamlit community!

This is one of our more requested features, but unfortunately, I don’t have an ETA when it might be implemented:

Another user wrote this workaround if you need this immediately:

Thanks for the reply!
My application has to reside on a private network so I’m not sure if the google analytics stuff can be run without sending any data outside?

In the meantime I took the idea from there to implement a simple counter.

My lack of Python skills show as I wasn’t able to make this a simple integer count, I had to append to the string and use len().

def Pageviews():
    return []


    st.markdown('Page viewed = {} times.'.format(len(pageviews)))
except ValueError:
    st.markdown('Page viewed = {} times.'.format(1))
1 Like

It’s more tedious than Google Analytics but you could try to run an instance of Matomo (it’s like Google Analytics but open source on prem) and then inject the JS snippet like in @randyzwitch’s answer.

I think you may be able to build a GlobalState by copying this gist near your file, and storing/incrementing your integer using an instance of this class. Something like :

import st_state_patch
s = st.GlobalState(key="user metadata")
if not s:
    # Initialize it here!
    s.counter = 0
s.counter += 1
st.markdown(f'Page viewed = {s.counter}')

I think you could do something that mimics server logs from a webserver…just write a line to a text file each time something happens, then count the events. That prevents a re-start from losing the counter information.

1 Like