With this repo, you can save your website visitors number on AWS DynomoDB and you can easily expose as api with AWS Lambda Function. If you want to add visitor number to your website you should use Javascript. In this repo, you will learn how to make visitor counter without using AWS Services.
First you should create an AWS account. After that you need to create a table named VisitorCount from DynamoDB and partition key must be id(String).
Than you should edit Item editor like this. After that you should create a Lambda and API Gateway.
This repo does not explain AWS setup. If you are having problems about setting up AWS Services. Please review the required documents.
First we are going to write a Lambda Function. Then you will install the lambda_function.py
. Import or copy everything like this.
After that create a Get Method in API Gateway. Connect the API Gateway with DynamoDB. After that you should Deploy API and Enable CORS.
If you're making the right adjustments, you need this building.
In this part, we should add Javascript command in our website code. You should Write this command inside the HTML code.
<script>
fetch('https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/beta') // You should change the link.
.then(response => response.json())
.then((data) => {
document.getElementById('visitor_counter').innerText = data.Count
})
</script>
With this command now you can call the value.
If you want to write this value to your website, you should add this command to your HTML code.
<span id="visitor_counter" />
After all of that, you can use visitor counter in your website.
You can see an example in here.