treosh / web-vitals-reporter

Report Web Vitals to your API with one POST request per session.
MIT License
239 stars 3 forks source link

load lib traditionally #5

Closed staabm closed 3 years ago

staabm commented 4 years ago

all examples shown use static imports to use the lib.

can the lib also be used in a classic way, using regular <script> tags?

alekseykulikov commented 3 years ago

Since v1, it's possible to load the lib with a <script> tag:

<script defer src="https://unpkg.com/web-vitals"></script>
<script defer src="https://unpkg.com/web-vitals-reporter"></script>
<script>
  addEventListener('DOMContentLoaded', function () {
    var sendToAnalytics = webVitalsReporter.createApiReporter('/analytics')
    webVitals.getCLS(sendToAnalytics)
    webVitals.getFID(sendToAnalytics)
    webVitals.getLCP(sendToAnalytics)
  })
</script>
alekseykulikov commented 3 years ago

It's also possible to load the library from unpkg.com using a module type:

<script type="module">
  import { getCLS, getFID, getLCP } from 'https://unpkg.com/web-vitals?module';
  import { createApiReporter, getDeviceInfo } from 'https://unpkg.com/web-vitals-reporter?module'

  // Init report callback with information about the browser.
  const sendToAnalytics = createApiReporter('/analytics', { initial: getDeviceInfo() })

  // Setup web-vitals
  getLCP(sendToAnalytics)
  getFID(sendToAnalytics)
  getCLS(sendToAnalytics)
</script>
staabm commented 3 years ago

Great, thx for the examples!