Skip to content

Commit

Permalink
Refactor out detector (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grunet committed May 18, 2023
1 parent 953fa78 commit 02d0b2d
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 170 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.6.0

- Factor out the information detection logic so anyone can use it (e.g. for 1st
party custom analytics)

## 0.5.0

- Add support for detecting if the user uses pinch zoom
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ The snippet should always be inserted as follows
The initial synchronous work done by the snippet takes approximately 15 plus or
minus 10 milliseconds, roughly.

The snippet itself currently weighs in at approximately 2.5 kilobytes
(approximately 1.1 kilobytes when compressed with gzip)
The snippet itself currently weighs in at approximately 3.2 kilobytes
(approximately 1.3 kilobytes when compressed with gzip)

## Build Pipeline

Expand Down
2 changes: 1 addition & 1 deletion dist/ga-analytics-escapedForBackticks.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(()=>{function v({getGlobal:r,setGlobal:d,translateArguments:u,syncItemsCallback:c,usesKeyboardCallback:f,usesPinchZoomCallback:l}){let o={},b=r();d(function(...t){let n=u({originalArguments:t,accessibilityEventParameters:o});b.apply(window,n)});try{a({mediaFeature:"prefers-reduced-motion",abbreviation:"prm",possibleValues:["no-preference","reduce"]}),a({mediaFeature:"prefers-color-scheme",abbreviation:"pcs",possibleValues:["light","dark"]}),a({mediaFeature:"inverted-colors",abbreviation:"ic",possibleValues:["none","inverted"]}),a({mediaFeature:"forced-colors",abbreviation:"fc",possibleValues:["none","active"]}),a({mediaFeature:"prefers-contrast",abbreviation:"pc",possibleValues:["no-preference","more","less","custom"]});let e=window.getComputedStyle(document.body).getPropertyValue("font-size");o.fs=e,c&&c()}catch(e){console.error(e)}(function(){try{let t=setInterval(function(){let i=document.querySelector(":focus-visible");if(!i)return;let s=i.tagName.toUpperCase();s==="INPUT"||s==="TEXTAREA"||s==="SELECT"||i.contentEditable!=="true"&&(o.uk=!0,clearInterval(t),f&&f())},500)}catch(t){console.error(t)}})(),function(){try{let t=window.visualViewport.scale,n=setInterval(function(){window.visualViewport.scale!==t&&(o.upz=!0,clearInterval(n),l&&l())},500)}catch(t){console.error(t)}}();function a({mediaFeature:e,abbreviation:t,possibleValues:n}){if(m({mediaFeature:e})===!1)return;let s=n.map(p=>({possibleValue:p,mediaQueryResult:window.matchMedia(\`(\${e}: \${p})\`).matches})).find(({_:p,mediaQueryResult:y})=>y===!0);if(s===void 0){console.error(\`Something went wrong. Is there a new \${e} allowed value not accounted for here?\`);return}o[t]=s.possibleValue}function m({mediaFeature:e}){return window.matchMedia(\`not all and (\${e}), (\${e})\`).matches?!0:(console.warn(\`Your browser doesn't support \${e} yet\`),!1)}}function w(){v({getGlobal:()=>window.gtag,setGlobal:r=>{window.gtag=r},translateArguments:({originalArguments:r,accessibilityEventParameters:d})=>{if(r[0]!=="event")return r;let u=r[1],c=Object.fromEntries(Object.entries(d).map(([b,a])=>[\`\${b}__\${u}\`.replaceAll("-","_").replaceAll(" ","_"),a])),l={...r[2],...c},o=[...r];return o[2]=l,o},syncItemsCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onSyncItemsResolved,usesKeyboardCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onUsesKeyboardResolved,usesPinchZoomCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onUsesPinchZoomResolved})}w();})();
(()=>{function u({onResolutionCallback:t}){try{s({mediaFeature:"prefers-reduced-motion",abbreviation:"prm",possibleValues:["no-preference","reduce"]}),s({mediaFeature:"prefers-color-scheme",abbreviation:"pcs",possibleValues:["light","dark"]}),s({mediaFeature:"inverted-colors",abbreviation:"ic",possibleValues:["none","inverted"]}),s({mediaFeature:"forced-colors",abbreviation:"fc",possibleValues:["none","active"]}),s({mediaFeature:"prefers-contrast",abbreviation:"pc",possibleValues:["no-preference","more","less","custom"]});let e=window.getComputedStyle(document.body).getPropertyValue("font-size");t({eventType:"data",source:"syncItems",data:{fs:e}}),t({eventType:"completionNotification",source:"syncItems"})}catch(e){console.error(e)}(function(){try{let r=setInterval(function(){let o=document.querySelector(":focus-visible");if(!o)return;let a=o.tagName.toUpperCase();a==="INPUT"||a==="TEXTAREA"||a==="SELECT"||o.contentEditable!=="true"&&(t({eventType:"data",source:"usesKeyboard",data:{uk:!0}}),clearInterval(r),t({eventType:"completionNotification",source:"usesKeyboard"}))},500)}catch(r){console.error(r)}})(),function(){try{let r=window.visualViewport.scale,n=setInterval(function(){window.visualViewport.scale!==r&&(t({eventType:"data",source:"usesPinchZoom",data:{upz:!0}}),clearInterval(n),t({eventType:"completionNotification",source:"usesPinchZoom"}))},500)}catch(r){console.error(r)}}();function s({mediaFeature:e,abbreviation:r,possibleValues:n}){if(l({mediaFeature:e})===!1)return;let a=n.map(i=>({possibleValue:i,mediaQueryResult:window.matchMedia(\`(\${e}: \${i})\`).matches})).find(({_:i,mediaQueryResult:c})=>c===!0);if(a===void 0){console.error(\`Something went wrong. Is there a new \${e} allowed value not accounted for here?\`);return}t({eventType:"data",source:"syncItems",data:{[r]:a.possibleValue}})}function l({mediaFeature:e}){return window.matchMedia(\`not all and (\${e}), (\${e})\`).matches?!0:(console.warn(\`Your browser doesn't support \${e} yet\`),!1)}}function f({getGlobal:t,setGlobal:s,translateArguments:l,syncItemsCallback:e,usesKeyboardCallback:r,usesPinchZoomCallback:n}){try{let o={},a=t();s(function(...c){let d=l({originalArguments:c,accessibilityEventParameters:o});a.apply(window,d)}),u({onResolutionCallback({eventType:i,source:c,data:d}){if(i==="data"&&Object.assign(o,d),i==="completionNotification")switch(c){case"syncItems":e&&e();break;case"usesKeyboard":r&&r();break;case"usesPinchZoom":n&&n();break;default:throw new Error(\`Source \${c} unknown\`)}}})}catch(o){console.error(o)}}function p(){f({getGlobal:()=>window.gtag,setGlobal:t=>{window.gtag=t},translateArguments:({originalArguments:t,accessibilityEventParameters:s})=>{if(t[0]!=="event")return t;let l=t[1],e=Object.fromEntries(Object.entries(s).map(([a,i])=>[\`\${a}__\${l}\`.replaceAll("-","_").replaceAll(" ","_"),i])),n={...t[2],...e},o=[...t];return o[2]=n,o},syncItemsCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onSyncItemsResolved,usesKeyboardCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onUsesKeyboardResolved,usesPinchZoomCallback:globalThis.a11y_analytics_config?.providers?.ga?.callbacks?.onUsesPinchZoomResolved})}p();})();
2 changes: 1 addition & 1 deletion dist/ga-analytics.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/plausible-analytics-escapedForBackticks.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(()=>{function f({getGlobal:t,setGlobal:d,translateArguments:c,syncItemsCallback:n,usesKeyboardCallback:p,usesPinchZoomCallback:u}){let r={},m=t();d(function(...o){let s=c({originalArguments:o,accessibilityEventParameters:r});m.apply(window,s)});try{i({mediaFeature:"prefers-reduced-motion",abbreviation:"prm",possibleValues:["no-preference","reduce"]}),i({mediaFeature:"prefers-color-scheme",abbreviation:"pcs",possibleValues:["light","dark"]}),i({mediaFeature:"inverted-colors",abbreviation:"ic",possibleValues:["none","inverted"]}),i({mediaFeature:"forced-colors",abbreviation:"fc",possibleValues:["none","active"]}),i({mediaFeature:"prefers-contrast",abbreviation:"pc",possibleValues:["no-preference","more","less","custom"]});let e=window.getComputedStyle(document.body).getPropertyValue("font-size");r.fs=e,n&&n()}catch(e){console.error(e)}(function(){try{let o=setInterval(function(){let l=document.querySelector(":focus-visible");if(!l)return;let a=l.tagName.toUpperCase();a==="INPUT"||a==="TEXTAREA"||a==="SELECT"||l.contentEditable!=="true"&&(r.uk=!0,clearInterval(o),p&&p())},500)}catch(o){console.error(o)}})(),function(){try{let o=window.visualViewport.scale,s=setInterval(function(){window.visualViewport.scale!==o&&(r.upz=!0,clearInterval(s),u&&u())},500)}catch(o){console.error(o)}}();function i({mediaFeature:e,abbreviation:o,possibleValues:s}){if(v({mediaFeature:e})===!1)return;let a=s.map(b=>({possibleValue:b,mediaQueryResult:window.matchMedia(\`(\${e}: \${b})\`).matches})).find(({_:b,mediaQueryResult:y})=>y===!0);if(a===void 0){console.error(\`Something went wrong. Is there a new \${e} allowed value not accounted for here?\`);return}r[o]=a.possibleValue}function v({mediaFeature:e}){return window.matchMedia(\`not all and (\${e}), (\${e})\`).matches?!0:(console.warn(\`Your browser doesn't support \${e} yet\`),!1)}}function h(){f({getGlobal:()=>window.plausible,setGlobal:t=>{window.plausible=t},translateArguments:({originalArguments:t,accessibilityEventParameters:d})=>{let c=[...t],n=t[1],u={...n?.props,...d},r={...n,props:u};return c[1]=r,c},syncItemsCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onSyncItemsResolved,usesKeyboardCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onUsesKeyboardResolved,usesPinchZoomCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onUsesPinchZoomResolved})}h();})();
(()=>{function d({onResolutionCallback:o}){try{s({mediaFeature:"prefers-reduced-motion",abbreviation:"prm",possibleValues:["no-preference","reduce"]}),s({mediaFeature:"prefers-color-scheme",abbreviation:"pcs",possibleValues:["light","dark"]}),s({mediaFeature:"inverted-colors",abbreviation:"ic",possibleValues:["none","inverted"]}),s({mediaFeature:"forced-colors",abbreviation:"fc",possibleValues:["none","active"]}),s({mediaFeature:"prefers-contrast",abbreviation:"pc",possibleValues:["no-preference","more","less","custom"]});let e=window.getComputedStyle(document.body).getPropertyValue("font-size");o({eventType:"data",source:"syncItems",data:{fs:e}}),o({eventType:"completionNotification",source:"syncItems"})}catch(e){console.error(e)}(function(){try{let t=setInterval(function(){let r=document.querySelector(":focus-visible");if(!r)return;let n=r.tagName.toUpperCase();n==="INPUT"||n==="TEXTAREA"||n==="SELECT"||r.contentEditable!=="true"&&(o({eventType:"data",source:"usesKeyboard",data:{uk:!0}}),clearInterval(t),o({eventType:"completionNotification",source:"usesKeyboard"}))},500)}catch(t){console.error(t)}})(),function(){try{let t=window.visualViewport.scale,a=setInterval(function(){window.visualViewport.scale!==t&&(o({eventType:"data",source:"usesPinchZoom",data:{upz:!0}}),clearInterval(a),o({eventType:"completionNotification",source:"usesPinchZoom"}))},500)}catch(t){console.error(t)}}();function s({mediaFeature:e,abbreviation:t,possibleValues:a}){if(c({mediaFeature:e})===!1)return;let n=a.map(i=>({possibleValue:i,mediaQueryResult:window.matchMedia(\`(\${e}: \${i})\`).matches})).find(({_:i,mediaQueryResult:l})=>l===!0);if(n===void 0){console.error(\`Something went wrong. Is there a new \${e} allowed value not accounted for here?\`);return}o({eventType:"data",source:"syncItems",data:{[t]:n.possibleValue}})}function c({mediaFeature:e}){return window.matchMedia(\`not all and (\${e}), (\${e})\`).matches?!0:(console.warn(\`Your browser doesn't support \${e} yet\`),!1)}}function p({getGlobal:o,setGlobal:s,translateArguments:c,syncItemsCallback:e,usesKeyboardCallback:t,usesPinchZoomCallback:a}){try{let r={},n=o();s(function(...l){let u=c({originalArguments:l,accessibilityEventParameters:r});n.apply(window,u)}),d({onResolutionCallback({eventType:i,source:l,data:u}){if(i==="data"&&Object.assign(r,u),i==="completionNotification")switch(l){case"syncItems":e&&e();break;case"usesKeyboard":t&&t();break;case"usesPinchZoom":a&&a();break;default:throw new Error(\`Source \${l} unknown\`)}}})}catch(r){console.error(r)}}function f(){p({getGlobal:()=>window.plausible,setGlobal:o=>{window.plausible=o},translateArguments:({originalArguments:o,accessibilityEventParameters:s})=>{let c=[...o],e=o[1],a={...e?.props,...s},r={...e,props:a};return c[1]=r,c},syncItemsCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onSyncItemsResolved,usesKeyboardCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onUsesKeyboardResolved,usesPinchZoomCallback:globalThis.a11y_analytics_config?.providers?.plausible?.callbacks?.onUsesPinchZoomResolved})}f();})();
Loading

0 comments on commit 02d0b2d

Please sign in to comment.