Skip to content

Instantly share code, notes, and snippets.

@SanichKotikov
Created December 23, 2019 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanichKotikov/d5ca82d534e79da1f67c2a0597ce9753 to your computer and use it in GitHub Desktop.
Save SanichKotikov/d5ca82d534e79da1f67c2a0597ce9753 to your computer and use it in GitHub Desktop.
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