Skip to content

Easily count site visitors with AWS Lambda and display it on your site.

Notifications You must be signed in to change notification settings

kuzeyardabulut/aws-visitor-counter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

What Is This?

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.

Creating 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).

image

Than you should edit Item editor like this. After that you should create a Lambda and API Gateway.

Lambda Function

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.

image

After that create a Get Method in API Gateway. Connect the API Gateway with DynamoDB. After that you should Deploy API and Enable CORS.

image

If you're making the right adjustments, you need this building.

Usage of HTML and Javascript

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.