hitcounter

package module
v0.0.0-...-6c2cd70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

README

hitcounter

Hitcounter is a simple web application for counting page views, written in Go. It's built for privacy-friendly environments and personal sites like the author's, when you just want to know that someone in the world saw your website.

The application compiles to a single binary that you can run anywhere. It stores everything in memory, then persists data to a single JSON file upon shutdown. No database or external dependencies are needed.

Getting Started

Build the application:

go get github.com/thebaer/hitcounter/cmd/hitcounter

Then run it:

hitcounter

Woohoo!

Embedding in your site

Embed the following code for invisible counting:

<img src="http://localhost:6767/hit.gif?p=PAGE/GOES/HERE" style="border:0;" alt="" />

Replace http://localhost:6767 with the domain where you're running this application.

Replace PAGE/GOES/HERE with the current page's path. It should be different for each page.

Hit counter

To show your visitors how many page views you've received, embed the following code, replacing certain parts as outlined in the instructions above.

<div id="count">Viewed ??? times</div>

<noscript><img src="http://localhost:6767/hit.gif?p=PAGE/GOES/HERE" style="border:0;" alt="" /></noscript>
<script>
    var xh = new XMLHttpRequest();
    xh.onreadystatechange = function() {
        if (xh.readyState == XMLHttpRequest.DONE && xh.status == 200) {
            document.getElementById('count').innerHTML = "Viewed " + xh.responseText + " times";
        }
    };
    xh.open("GET", "http://localhost:6767/hit?p=PAGE/GOES/HERE", true);
    xh.send();
</script>

API

All endpoints respond to GET requests.

/hit?p=KEY

This endpoint counts a new view for the given KEY and then returns the total count as plain text.

/hit.gif?p=KEY

This endpoint counts a new view for the given KEY, serving an invisible pixel.

/hits?p=KEY

This endpoint returns the total count for the given KEY as plain text without recording a new view.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountView

func CountView(r *http.Request) bool

CountView counts the view if it meets certain criteria, returning whether or not it was actually counted.

func SaveCounts

func SaveCounts() error

func Serve

func Serve() error

Initialize and start web server

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL