Use Vercel Analytics with no frameworks

Hey, so I’m trying to implement Vercel Analytics in my vercel project. I saw some examples, but they used ES6 method to import the inject function. I try to use import function, here is my code:

<!-- ... -->
<script type="module"> 
     import { inject } from '@vercel/analytics';
      
     inject();
</script>
<!-- ... -->

And this is the error:

Uncaught TypeError: Failed to resolve module specifier "@vercel/analytics". Relative references must start with either "/", "./", or "../".

I installed the package using npm install @vercel/analytics.

Hi, @ryanbaig!

The error you’re encountering may be because you’re trying to use a Node.js-style import in a browser environment. Browsers don’t have direct access to the node_modules folder, so they can’t resolve the @vercel/analytics package.

To correctly implement Vercel Analytics in your project, you should:

  1. First, make sure you have installed the package correctly:
npm install @vercel/analytics
  1. Instead of trying to import the package directly in a script tag, you should use the Vercel Analytics React component in your main layout file. This approach uses the Analytics component from @vercel/analytics/react, which is designed to work with React applications deployed on Vercel .

  2. After adding this component to your layout, deploy your application to Vercel. The analytics will start working automatically once your site is live on Vercel.

  3. Make sure you have enabled Web Analytics in your Vercel project settings. You can do this by going to your project dashboard on Vercel, clicking on the Analytics tab, and enabling it from there .

Let us know how you get on!

Hey @pawlean!
Thanks for responding. I just wanted to tell you that I am not using React or any other framework.
Here’s my project structure if ur wondering:

- .vercel/ (vercel CLI stuff)
- api/ (My APIs)
- assets/ (CSS, JS, etc)
- node_modules/ (self-explanatory)

- pages/ (my HTML files)
-- pages/login.html
-- pages/dashboard.html

- package.json, package-lock.json (again, self-explanatory)
- vercel.json

So, I’m not using any framework, please let me know if there’s a way to include vercel analytics in a project with no frameworks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.