1

I'm trying to record events using the JS Amplitude API.

The setup instructions are to put code in the <head> of pages. See Full instructions here.

Since I'm using React, I put the code in index.html

The event I'm trying to record is executed on componentDidMount on various pages:

componentDidMount() {
amplitude.getInstance().logEvent("test_profile_accessed", {page: `${test_info}`});
}

However, sometimes the event doesn't record because I get this error:

[Amplitude] Invalid apiKey. Please set a valid apiKey with init() before calling logEvent()

This error only happens every once in a while, with the other times successfully recording.

What is going on? I don't see any documentation about this error anywhere else.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Michael Ninh
  • 772
  • 2
  • 10
  • 23

2 Answers2

0

As the docs state you have to set API key that you got after creating a project:

  1. Insert your API Key. Replace API_KEY with the API Key given to you. You can find your project's API Key in your project's Settings page.
<script type="text/javascript">
    // removed the code that loads the Amplitude JavaScript SDK
    amplitude.getInstance().init("API_KEY");
</script>

Above script should be included inside your index.html head. Make sure you replace API_KEY with your actual API key.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
  • Hey HC - I do have the correct API key entered. The events are currently logging only around 70% of the time right now. The rest is getting the error. – Michael Ninh Apr 16 '20 at 18:06
  • @MichaelNinh This is important information to add to your question. May be the unreliable/occasional error is caused by API (key) dependent _rate limits_ (requests per day)? – hc_dev Apr 16 '20 at 19:23
0

Had this issue with @amplitude/analytics-browser. Solved by setting the serverZone to the correct zone (EU in my case)

  amplitude.init("YOUR_AMPLITUDE_API_KEY", undefined, { serverZone: "EU" });
Bassem
  • 3,582
  • 2
  • 24
  • 47