This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adds web-vitals and greater analytics tracking * Add attribution * Linting fixes * More linting * Fix Navigation Type * Add JSON to prod build too * Linting fixes * Fix types * Review feedback * Updated plugin-json comment
- Loading branch information
1 parent
9a9c8a1
commit 69957ba
Showing
8 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
{ | ||
"id": "UA-41980257-1", | ||
"dimensions": { | ||
"TRACKING_VERSION": "dimension1" | ||
"TRACKING_VERSION": "dimension1", | ||
"NAVIGATION_TYPE": "dimension2", | ||
"WEB_VITALS_DEBUG": "dimension3" | ||
}, | ||
"TRACKING_VERSION": "2.0" | ||
} | ||
"TRACKING_VERSION": "3.0" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { | ||
onCLS, | ||
onFCP, | ||
onFID, | ||
onLCP, | ||
onTTFB, | ||
onINP, | ||
} from 'web-vitals/attribution'; | ||
import {dimensions} from '../_data/analytics.json'; | ||
|
||
/** | ||
* See: https://github.com/GoogleChrome/web-vitals#using-analyticsjs | ||
* @param {Object} metric | ||
*/ | ||
function sendToGoogleAnalytics({name, delta, id, attribution, navigationType}) { | ||
let webVitalInfo = '(not set)'; | ||
|
||
switch (name) { | ||
case 'CLS': | ||
webVitalInfo = attribution.largestShiftTarget; | ||
break; | ||
case 'FID': | ||
case 'INP': | ||
webVitalInfo = attribution.eventTarget; | ||
break; | ||
case 'LCP': | ||
webVitalInfo = attribution.element; | ||
break; | ||
} | ||
|
||
// Assumes the global `ga()` function exists, see: | ||
// https://developers.google.com/analytics/devguides/collection/analyticsjs | ||
ga('send', 'event', { | ||
eventCategory: 'Web Vitals', | ||
eventAction: name, | ||
// Google Analytics metrics must be integers, so the value is rounded. | ||
// For CLS the value is first multiplied by 1000 for greater precision | ||
// (note: increase the multiplier for greater precision if needed). | ||
eventValue: Math.round(name === 'CLS' ? delta * 1000 : delta), | ||
// The `id` value will be unique to the current page load. When sending | ||
// multiple values from the same page (e.g. for CLS), Google Analytics can | ||
// compute a total by grouping on this ID (note: requires `eventLabel` to | ||
// be a dimension in your report). | ||
eventLabel: id, | ||
// Use a non-interaction event to avoid affecting bounce rate. | ||
nonInteraction: true, | ||
|
||
// See: https://web.dev/debug-web-vitals-in-the-field/ | ||
[dimensions.WEB_VITALS_DEBUG]: webVitalInfo, | ||
[dimensions.NAVIGATION_TYPE]: navigationType, | ||
}); | ||
} | ||
|
||
/** | ||
* Add a listener to detect back/forward cache restores and track them | ||
* as pageviews with the "bfcache" navigation type set (in case we need | ||
* to distinguish them from regular pageviews). | ||
* https://web.dev/bfcache/#how-bfcache-affects-analytics-and-performance-measurement | ||
*/ | ||
window.addEventListener( | ||
'pageshow', | ||
/** | ||
* @param {PageTransitionEvent} e | ||
*/ | ||
e => { | ||
if (e.persisted) { | ||
ga('set', dimensions.NAVIGATION_TYPE, 'back-forward-cache'); | ||
ga('send', 'pageview'); | ||
} | ||
} | ||
); | ||
|
||
onCLS(sendToGoogleAnalytics); | ||
onFCP(sendToGoogleAnalytics); | ||
onFID(sendToGoogleAnalytics); | ||
onINP(sendToGoogleAnalytics); | ||
onLCP(sendToGoogleAnalytics); | ||
onTTFB(sendToGoogleAnalytics); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare module 'webdev_analytics' { | ||
export declare const id: string; | ||
export declare const dimensions: { | ||
TRACKING_VERSION: 'dimension1'; | ||
NAVIGATION_TYPE: 'dimension2'; | ||
WEB_VITALS_DEBUG: 'dimension3'; | ||
}; | ||
export declare const version: number; | ||
} |