Page view counter

Has anybody ever incorporated a page view counter into one of their apps? Would love to have the functionality but without it being a core component, I wouldn’t know how to make one.

If you search something like “free visitor counter” you’ll find a bunch of options. They generally give you a chunk of HTML to add to your site. You could either add it to the page load template by writing a custom index string or replicate the HTML with dash-html-components, either should work.

Alternatively, you could keep track of the number of visitors yourself. Dash allows you to assign a function to app.layout which gets executed on each page load. When a user visits the site you could simply increment the count. Here’s an example to illustrate what I’m talking about, but for reasons explained below don’t actually use this code


def count_views():
    global VIEWS
    VIEWS += 1
    return html.Div(...)  # your layout

app.layout = count_views

As mentioned, this is a bad idea, specifically the use of global variables. See here for more details.

Instead you should replace the global variable above with a variable read either from disk or from a shared memory store. Some options might be: