Count API

Lyket Count API: Simplifying Counting with an Intuitive API

0

Counting is a fundamental operation in many applications, ranging from tracking website hits to monitoring user interactions. With the advent of APIs, developers can now leverage powerful tools to simplify and streamline their counting needs. One such API, Lyket count API, stands out as a versatile and user-friendly solution. In this article, we will explore the capabilities of our count API and how it can be used to effortlessly count anything.

Getting Started

To ensure the security and integrity of your counters, Lyket requires identity verification. As a result, you need to provide a unique alphanumeric value known as the public API key that belongs only to you. Obtaining the public API key involves registering with Lyket. Once you have completed the registration process, you will receive the necessary API key to authenticate your website and enable Lyket functionality.

Create a Counter

To begin using the Count API, you need to create a new counter, in Lyket they are called buttons, because they trigger on user interaction. Each button can increment, decrement, and retrieve the current value of any event. All counters are accessible using a label, that is constitued by a unique key within a namespace. Within each namespace, you can generate as many counters as necessary to suit your counting requirements. To create a counter, simply make any request to the Lyket API using the unique namespace/id combination.

For example, making a request to https://api.lyket.dev/v1/clap-buttons/user-visits/my-first-blog-post will automatically create a counter with ID "user-visits/my-first-blog-post".

Choose Counter Behaviour

Depending on the metric you want to track and count, you may need different type of counting behaviour. Lyket count API provides four different style of counting.

  • Clap counters: the same user can increment the counter multiple times. This is useful to track user visits to your webpages.
  • Like/dislike counters, also known as Updown counters: users can either add or subtract one to the counter total, once. This is useful to count unique visits, or to track scores that can also have negative value.
  • Rating counters: users can add any value to the counter total, once. This is useful to weight your scores, according to a custom logic.
  • Like counters: users can only increment each counter by one. It allows also to rollback and remove the user plus one. This is useful to count unique visits or to track votes.

To read the list of all the possible actions that our API offers, please visit our API documentation page

Getting Current Value of a Counter

Upon making a request to the endpoint https://api.lyket.dev/v1/clap-buttons/user-visits/my-first-blog-post , you will receive a response indicating the current value of the counter. This information can be utilized to display real-time statistics or provide valuable insights into user engagement.

<!DOCTYPE html>
<html>
<head>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      const counterUrl = "https://api.lyket.dev/v1/clap-buttons/my-page/my-button"; // Replace with your counter ID
      const apiKey = "YOUR_API_KEY"; // Replace with the API key you got from registration

      const options = {
        headers: {
          "Authorization": `Bearer ${apiKey}`
        }
      };

      fetch(counterUrl, options)
        .then(response => response.json())
        .then(({ data }) => {
          const counter = document.getElementById("counter");
          counter.innerHTML = `Counter: ${data.attributes.total_claps}`;
        })
        .catch(error => {
          console.error("Error:", error);
        });
    });
  </script>
</head>
<body>
  <div id="counter"></div>
</body>
</html>

Incrementing Counters Using Buttons

One of the most common use cases for a count API is counting each time a user has clicked a button or interacted with some element in your webpage. We can use the clap behaviour to track everytime a user hits a button and increment the total counter by one, like in this example:

<!DOCTYPE html>
<html>
<head>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      const counterUrl = "https://api.lyket.dev/v1/clap-buttons/my-page/my-button/press";  // Replace /my-page/my-button with your counter ID
      const apiKey = "YOUR_API_KEY"; // Replace with the API key you got from registration

      const options = {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "Authorization": `Bearer ${apiKey}`
        }
      };

      const button = document.getElementById("counterButton");

      button.addEventListener("click", function() {
        fetch(counterUrl, options)
          .then(response => response.json())
          .then(({ data }) => {
            console.log("API response:", data);
            const counterContainer = document.getElementById("counter");
            counterContainer.innerHTML = `Counter: ${data.attributes.total_claps}`;
          })
          .catch(error => {
            console.error("Error:", error);
          });
      });
    });
  </script>
</head>
<body>
  <button id="counterButton">Increment Counter</button>
  <div id="counter"></div>
</body>
</html>

If you want to use our out-of-the-box Lyket buttons, checkout our HTML widget that provides also a nice looks to your counter buttons.

Track User Visits

Anothe common use cases for a Count API is incrementing a counter to track the occurrence of visits to a webpage. Even if Lyket count API is made to track button clicks, the API could also easily be used as a straightforward endpoint for incrementing any counter. Each time this endpoint is requested, the corresponding counter will increase by one. For example, to track the number of hits a page receives, you can use the behaviour associated to the clap button, where you can increment endlessly a counter. Use the following code to implement a visit tracking counter:

<!DOCTYPE html>
<html>
<head>
  <script>
    document.addEventListener("DOMContentLoaded", function() {
      const counterUrl = "https://api.lyket.dev/v1/clap-buttons/visits/name-of-my-page/press";
      const apiKey = "YOUR_API_KEY"; // Replace with the API key you got from registration

      const options = {
        method: "PUT",
        headers: {
          "Content-Type": "application/json",
          "Authorization": `Bearer ${apiKey}`
        }
      };

      fetch(counterUrl, options)
        .then(response => response.json())
        .then(({ data }) => {
          const resultContainer = document.getElementById("result");
          resultContainer.innerHTML = `Total visits to this page: ${JSON.stringify(
            data.attributes.total_claps,
          )}`;
        })
        .catch(error => {
          console.error("Error:", error);
        });
    });
  </script>
</head>
<body>
  <div id="result"></div>
</body>
</html>

Why Using a Count API?

Lyket count API's versatility allows it to be utilized in a wide range of applications. Some possible use cases include:

  • Website Analytics: Track the number of visits, unique visitors, or button clicks on your website.
  • Social Media Metrics: Monitor the number of likes, shares, or comments on a post or a tweet.
  • Application Usage: Count the number of times a feature or functionality is accessed within your application.
  • Gaming Statistics: Keep track of high scores, achievements, or player interactions in games.

Conclusion

Lyket's count API provides a hassle-free solution for counting and tracking various events or interactions. By leveraging its intuitive API, and providing different behaviours for different necessities, developers can easily create, manage, and retrieve counters for any purpose, with flexibility and control. Whether you need to monitor website hits, user interactions, or any other numerical count, our count API simplifies the process and empowers developers to focus on building innovative applications.