What is the method done by Bitly to track clicks

I am trying to implement a code that detects a Bitly link and track users based on there mobile device (Android, IOS, Website) e.g I want to have the count of android users, Apple users, and website users that clicked on the Bitly link so here is my code

<script type="text/javascript">
    function getMobileOperatingSystem() {
        var userAgent = navigator.userAgent || navigator.vendor || window.opera;
        var url = "https://nch.mobi/334NXbn";
        var agnt = "web";
        var pge = "@ViewBag.campaignId";
        var link = '@Url.Action("_AddCount", "Home")';
        // Windows Phone must come first because its UA also contains "Android"
        if (/windows phone/i.test(userAgent)) {
            agnt="Windows Phone"
        }// android
        else if (/android/i.test(userAgent)) {
            url = "https://nch.mobi/3m1PM1q";
            agnt = "Android";
        // iOS
        else if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
            url = "https://nch.mobi/35dOPNl";
            agnt = "IOS"

            type: "POST",
            url: link,
            data: AddFormAntiForgeryToken({ 'campaign': pge, 'agent': agnt }),
            dataType: "json",
            success: function (response) {
                if (response.success) {
                    window.location.href = url;
                else {
                    alert("Error occured.");
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(textStatus, errorThrown);
    function AddFormAntiForgeryToken(data) {
        data.__RequestVerificationToken = $("input[name='__RequestVerificationToken']").val();
        return data;


    public ActionResult _AddCount(string campaign, string agent)
        CountTableHelper hlpe = new CountTableHelper();
        var t = new CountTable
            Agent = agent,
            CampaignId = Convert.ToInt32(campaign),
            CreatedDate = DateTime.UtcNow,
            IpAddress = GetIpAddress()                

        return Json(new { success = true }, JsonRequestBehavior.AllowGet);

But in my case, the total that shows in Bitly should be the same total that I am receiving in my DataBase, but it is not the case, Though when a refresh my page a new click is tracked but on Bitly it is not the case how can I fix my logic to get the same clicks count?

I appreciate your help.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your flaw is that bitly uses it’s own system. When you click a bitly link. You briefly hit the bitly servers, bitly logs this then redirects you to where you want to go.

Bitly likely also as measures to “prevent abuse” (along other things). Refreshing the page probably trips this, and it doesn’t log another visit. There are a bunch of ways it could handle that, so no clue which one(s) it uses. Bitly has it’s own business needs as far as how it tracks links. These needs may not be identical to yours.

It’s also possible Bitly uses eventual consistency in how it computes the total links. Meaning it make log the click but not immediately update the page that displays the count.

Though the real question might be why are you counting it twice? Why not use Bitly’s API to get the counts directly since you already have separate links setup for each? If you prefer your numbers over Bitly’s then you likely don’t need to worry too much about Bitly’s.

My recommendation? Either use bitly’s API to get the total or use your own numbers. Your own numbers give you more flexibility as you can potentially filter out things by user agent and IP address and filter out duplicate clicks, though likely Bitly already does this to a fair degree.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x