Search code examples
c#jqueryasp.netasp.net-mvcbitly

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">
    getMobileOperatingSystem();
    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"
        }


        $.ajax({
            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;
    }
</script>

Backend

[HttpPost]
    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()                
        };
        hlpe.Create(t);

        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.


Solution

  • 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.