Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 19x 19x | import { useCallback } from "react"; import { dataToSendType, Fields, UseAnalyticsParams } from "types/analytics"; import { makeCookie, takeCookie } from "utils"; function addAnalyticsCookie() { const value = `GA1.2.${~~(2147483648 * Math.random())}.${~~( Date.now() / 1000 )}`; makeCookie({ name: "_ga", value: value, age: 60 * 60 * 24 * 365, }); return value; } export function useAnalytics(): UseAnalyticsParams { const trackWithGoogleAnalytics: UseAnalyticsParams["trackWithGoogleAnalytics"] = useCallback((hitType = "pageview", fields: Fields) => { const analyticsCookie = takeCookie("_ga") ?? addAnalyticsCookie(); const data: Partial<dataToSendType> = { v: "1", tid: "UA-177844057-2", aip: "1", cid: analyticsCookie, t: hitType, dr: document.referrer, dt: document.title, dl: location.href, ul: navigator.language.toLowerCase(), sr: `${screen.width}x${screen.height}`, vp: `${innerWidth}x${innerHeight}`, }; Iif (fields && "eventCategory" in fields) { data.ec = fields.eventCategory; data.ea = fields.eventAction; data.el = fields.eventLabel; data.ev = fields.eventValue; } Iif (fields && "screenName" in fields) { data.cd = fields.screenName; } Iif (fields && "timingCategory" in fields) { data.utc = fields.timingCategory; data.utv = fields.timingVar; data.utt = fields.timingValue; data.utl = fields.timingLabel; } Iif (fields && "exDescription" in fields) { data.exd = fields.exDescription; data.exf = fields.exFatal; } Iif (fields && "socialNetwork" in fields) { data.sn = fields.socialNetwork; data.sa = fields.socialAction; data.st = fields.socialTarget; } navigator.sendBeacon( "https://google-analytics.com/collect", new URLSearchParams(data) ); }, []); return { trackWithGoogleAnalytics: trackWithGoogleAnalytics, }; } |