We are building our tracking tool, and we want to keep track of the CTA's on our website.
Here is the code for that:
<script type="text/javascript">
var $ = jQuery;
$("a").click(function () {
var link = $(this);
var href = link.attr('href');
var noProtocol = href.replace(/http[s]?:\/\//, '');
$.ajax({
type: 'GET',
url: 'https://ipinfo.io/json?token=369f3980fa1a62',
success: function (response) {
$.ajax({
type: 'POST',
url: 'https://myserver.com/api/events',
headers: {},
crossDomain: true,
data: JSON.stringify({
'distinct_id': '',
'name': 'button_clicked',
'page': noProtocol,
'domain': document.domain,
'referrer': document.referrer,
'ip_address': response.ip,
'region': response.region,
'postal': response.postal,
'location': response.loc,
'country': response.country,
'city': response.city
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
//do nothing
console.log(msg);
}
});
}
});
});
</script>
When someone clicks the link, it starts to post to our server but because tag redirects the page, the request is canceled. How to avoid this request is being canceled?