Created
December 23, 2019 16:05
-
-
Save SanichKotikov/d5ca82d534e79da1f67c2a0597ce9753 to your computer and use it in GitHub Desktop.
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
const URL = 'https://www.google-analytics.com/collect'; | |
export function ga(category: string, action: string) { | |
return new Promise((res) => { | |
const data = { | |
v: 1, | |
tid: 'UA-XXXXX-Y', | |
t: 'event', | |
ec: category, | |
ea: action, | |
}; | |
const params = new URLSearchParams(); | |
Object.keys(data).forEach(key => { | |
params.append(key, (data as any)[key]); | |
}); | |
const xhr = new XMLHttpRequest(); | |
xhr.open('POST', URL, true); | |
xhr.setRequestHeader('content-type', 'text/plain'); | |
xhr.send(params.toString()); | |
xhr.onreadystatechange = () => { | |
if (xhr.readyState !== 4) return; | |
res(xhr); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment