Monday, 14 September 2020

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.



from What is the method done by Bitly to track clicks

No comments:

Post a Comment