I find there are several google analytics alternative on the internet.

Now, I wonder if I can create my own google analytics? How to?

Hi,

do you want to build it from scratch or you want to install a monitoring software?

In the first case you can parse the access logs of the server and use javascript to catch events on the page. Read the documentation of the web server in use to see how data is logged and which features are available, for Apache read this:

I suggest you to do some research, there are a lot of articles on this topic.

If, instead, you want to install a monitoring software on the server, then look at tools like GoAccess:

you can get the results as JSON or CSV to create your own templates, or you can use the available interfaces.