-
-
Save Shoora/9b7e4d52b2fece60bdfba74d3f7e8bb8 to your computer and use it in GitHub Desktop.
Add GA event tracking on widget retailer links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Please ask Hatch to provide a list of all retailer IDs vs names | |
const retailer_mapping = { | |
94349 : 'MSI Store', | |
89105 : 'Walmart', | |
}; | |
//Listener on any hatch links | |
document.addEventListener( "click", hatchRetailerWidgetClick ); | |
function hatchRetailerWidgetClick(ev){ | |
const el = ev.target; | |
const hatch_link = el.closest('a[href*="https://gethatch.com/wtbonline"]'); | |
if(hatch_link !== null){ | |
console.log('Click on a Hatch link : ', hatch_link); | |
//extract retailer ID | |
const reg = /.+?:\/\/.+?(\/.+?)(?:#|\?|$)/; | |
const pathname = reg.exec(hatch_link)[1]; | |
const path_arr = pathname.split('/'); | |
//Retailer ID | |
const retailer_id = path_arr[3]; | |
console.log('retailer ID : ', retailer_id); | |
//Retailer name | |
const retailer_name = ((retailer_mapping[retailer_id] !== undefined)?retailer_mapping[retailer_id]:'NA'); | |
console.log('retailer name : ', retailer_name); | |
//Add gtag event, make sure you add the gtag core script as well | |
gtag('event', 'ps_retailer_click', { | |
'retailer_id': retailer_id, | |
'retailer_name': retailer_name, | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment