Occasional Amplitude "Invalid apiKey" - javascript

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.

As the docs state you have to set API key that you got after creating a project:
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.

Related

Recaptcha v3 initialization interrupted

Recaptcha v3 initialization is not completing when using the production site.
The logo is no longer appearing and if I try to submit the form I'm getting this error : "Uncaught Error: Invalid site key or not loaded in api.js"
It's working when I'm using it on localhost from my PC
The only difference between the dev and the prod are the keys.
Any idea to why it's not loading properly ?
Changed the keys.
Tried on other web browsers.
There's 2 main ways I believe this is caused:
Your domain name should just be the domain. apple.com, not https://apple.com
Make sure you are including the sitekey in the render param when you add the script tag:
<script src="https://www.google.com/recaptcha/api.js?render=<v3_site_key>"></script>
I've gotten the error when I forgot to provide the site key here (originally thought I only had to pass it in as the first argument to grecaptcha.execute)

How to implement Google Analytics event tracking inside a React VR project?

This is loosely written to give a basic idea of what I'm trying achieve.
< VrButton onClick={props.userClick}>< /VrButton>
userClick={() => this.triggerTracking}
triggerTracking() {
ga('send', 'event', 'myEventCategory', 'myEventAction', 'myEventLabel');
}
I expect the code to trigger Google Analytics event tracking in the GA system when the user clicks on a button, but I get an error message - "ga is not a function".
I have GA set up in my index.html file, with the proper ID, and pulling in the latest analytics.js API.
React VR is all within a web worker context so it is not possible to access anything on your window without the use of native modules.
You can embed them directly in your client js and use the GA tracking functions as you normally would there. You will then call a function on your native module within your react VR app.
You can check out the documentation here: https://facebook.github.io/react-vr/docs/native-modules.html
Try using the window scope as:
window.ga('send', 'event', 'myEventCategory', 'myEventAction', 'myEventLabel');
I'm not familiar with React at all, but perhaps React causes some abstraction between the window and the react scope, making your ga() function unavailable.
Do next stps:
open the network debug tool of your browser.
reload your page
review loaded url list and check that www.google-analytics.com/analytics.js is loaded
If you does not see such url loaded - read google analitycks manual about how to setup google analytics on your page.
If you see such url replace url www.google-analytics.com/analytics.js with www.google-analytics.com/analytics_debug.js in your page, than reload and than go to console tab of your browser debugger and check the errors.

Google Sheets API node.js quickstart won't work: The API returned an error: Error: unauthorized_client

I've been messing with the Google Sheets API for a project and everything has been going pretty well. I followed all the steps On this page and got the quickstart working great for a while... Until I ran into an issue where I was getting the error message API returned an error: Error: The request does not have valid authentication credentials.
To resolve this I tried a bunch of different things from changing the SCOPES variable to commenting and uncommenting out things.
Eventually I deleted the project and credentials on my google developers console account and tried to make a new one.
Whenever I start a new folder from scratch, make a quickstart.js file, install the node modules and run it, now I get The API returned an error: Error: unauthorized_client. I've made completely new client_secret.json files and made the quickstart.js from scratch directly using the quickstart walkthrough code and it won't work. What could be causing this?
Link to github (this is the code that WAS mostly working until I tried to delete rows from my google sheet after copying data to my sql database).
You may want to try what was done in this SO post wherein this hidden file has been deleted:
/Users/user/.credentials/gmail-nodejs-quickstart.json
In your case, you need to locate the path of sheets.googleapis.com-nodejs-quickstart.json then delete.
For this, you might need to also do a console.log to locate the correct path.

IN.User is undefined in LinkedIn Javascript SDK

I am trying to log the user in programatically using LinkedIn Javascript SDK. I am not using the default flow (which includes the button as a script) as we need a custom size button, so here is my code:
//inside head...
<script type="text/javascript" src="//platform.linkedin.com/in.js">
api_key: 'XXXXXXXXXXXX'
credentials_cookie: true
</script>
//inside some script tag
function loginWithLinkedIn() {
IN.User.authorize(linkedInLoginCallback); //yeah I have that method
}
However, as soon as call loginWithLinkedIn, I'm getting an error telling IN.User is undefined. IN itself is defined, but has only ENV and Event fields defined. How can I initiate LinkedIn login using JS (not a button/script tag)? I couldn't find any useful documentation anywhere, and LinkedIn's own documentation is terrible.
I was having the same issue. For me it was because we had changed the subdomain location of our application from www. to app. This change required me to go into the LinkedIn developer console and under the Javascript tab add our new app location to the list of Valid SDK Domains.

Uncaught TypeError when using Google API JavaScript Client

I am developing a simple Tic Tac Toe on Google App engine using cloud endpoints. My API name is tictactoe2D.
I have only 1 endpoint method by the name tictactoe2D.compute2DMove() in my API which I have tested in the APIs explorer, and is working absolutely fine.
Now I am working to create the front end of the game, and using Google APIs JavaScript Client library to communicate with my endpoint method. I have followed exactly the same procedure as shown in the Getting Started page, which is a complete tutorial on using the library.
Here is code snippet from board.html, which loads the Javascript library-
<head>
<title>Tic Tac Toe 3D</title>
<meta http-equiv="content-type" charset="utf-8" content="text/html"/>
<link href="css/main.css" rel="stylesheet" type="text/css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://apis.google.com/js/client.js?onload=onLoadCallback"></script>
<script type="text/javascript">
function onLoadCallback() {
gapi.client.setApiKey('AIzaSyB7p7mH_vZGgtrbF4ntmKN2nBcsRyRFY1w');
gapi.client.load('tictactoe2D', 'v1');
}
</script>
<script type="text/javascript" src="js/render.js"></script>
</head>
And here is the code from render.js, which add the functionality to deal with click event of all squares, and also communicates with my endpoint method-
$(document).ready(function() {
$("#board td").click(function() {
$(this).html("X");
var boardString = [];
var buttons = document.querySelectorAll('td');
for (var i=0; i<buttons.length; i++) {
boardString.push(buttons[i].innerHTML);
}
boardString.join('');
gapi.client.tictactoe2D.compute2DMove({'state': boardString}).execute(function(resp) {
document.write(resp.result.state);
});
});
});
Here is the JSFiddle for the whole code.
Problem occurs when I try to click one of the squares on the game board. Unexpectedly, nothing happens.
I opened up the JavaScript console in Chrome, and found out that the console showed the following error whenever I click on a square-
Uncaught TypeError: Cannot read property 'compute2DMove' of undefined
I have not been able to figure out why is this happening, and how can I fix it. Can anybody please help me out?
Thankyou very much in advance.
I ended up fixing the bug myself after I observing a new error on the JS console- The error was-
Failed to load resource: The server responded with a status of 403 (Forbidden)
I checked the JSON object that the server returned, and the object was something like this-
{"error":{"errors":[{"domain":"global","reason":"sslRequired","message":"SSL is required to perform this operation."}],"code":403,"message":"SSL is required to perform this operation."}}
After seeing this, I guessed that a secure connection(HTTPS) was not used to connect to the app, and that is why server was not loading the required data. To fix this, I needed to mandate that a secure connection be used every time the app is loaded. For this, I added the following tag to web.xml-
<security-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
I updated my app on appspot, and checked the JS console. There was no error there, and my endpoint method executed too. I hope this information if helpful to anyone who faces a similar issue.
Also, I found out that the gapi library had some stability issues in the past, and the best way to safeguard your API calls against any possible flaws is to construct a REST request using gapi.client.request(), instead on making JSON-RPC requests. (see, Constructing REST requests using gapi.client.request). Constructing REST requests might be lengthy and cumbersome, but is safe too.

Categories

Resources