$(document).ready(function() { function callGtag(eventName, eventData) { if(typeof gtag != "undefined") { gtag('event', eventName, eventData); } } $(document).on("ajaxSend", function(e, xhr, req) { if(req.type == 'POST') { if(req.url.includes('api/contact/sendform')) { xhr.then(function(response, status) { const resData = JSON.parse(response); console.log('then', status, resData); if(resData.status == 1 || resData.status == 42) { // Redirect to thanks page. } }); callGtag('contact_form_legacy', {}); console.log('gtag event sent'); } } else if(req.type == 'GET') { if(req.url.includes('api/search/')) { const urlP = new URL(location.origin + req.url); if(urlP.searchParams.get('text')) { callGtag('search', { search_text: urlP.searchParams.get('text') }); console.log('gtag event sent'); } } } }); });