initMetric.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { WINDOW } from '../../types.js';
  2. import { generateUniqueID } from './generateUniqueID.js';
  3. import { getActivationStart } from './getActivationStart.js';
  4. import { getNavigationEntry } from './getNavigationEntry.js';
  5. /*
  6. * Copyright 2020 Google LLC
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * https://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. const initMetric = (name, value) => {
  21. const navEntry = getNavigationEntry();
  22. let navigationType = 'navigate';
  23. if (navEntry) {
  24. if (WINDOW.document.prerendering || getActivationStart() > 0) {
  25. navigationType = 'prerender';
  26. } else {
  27. navigationType = navEntry.type.replace(/_/g, '-') ;
  28. }
  29. }
  30. return {
  31. name,
  32. value: typeof value === 'undefined' ? -1 : value,
  33. rating: 'good', // Will be updated if the value changes.
  34. delta: 0,
  35. entries: [],
  36. id: generateUniqueID(),
  37. navigationType,
  38. };
  39. };
  40. export { initMetric };
  41. //# sourceMappingURL=initMetric.js.map