felix_xiao felix_xiao - 13 days ago 7
HTML Question

React.js: Use Header-Imported Javascript Library

I have built a React.js web app and am trying to use Amplitude Analytics, which has a Javascript SDK here:

The instructions say to include a

<script></script>
in the
<head></head>
part of the page. I've added the script in my
index.html
page, and was wondering how I can use the library in my
.jsx
files.

This is an event tracking library, and to record an event, I need to call
amplitude.getInstance().logEvent('EVENT_IDENTIFIER_HERE');
. However, I'm not sure how to import
amplitude
so that it becomes available in my
Main.jsx
or my
Page1Component.jsx
file?

Thanks!!

Answer

It has been suggested to: npm install amplitude/Amplitude-Javascript --save which would install the library from github instead from the npm registry.

Then you can import it in your code:

import amplitude from 'amplitude-js/amplitude.min'

Reference: https://github.com/amplitude/Amplitude-Javascript/issues/40