I’m trying to do some work with Google Analytics. The google js that you load adds a gapi variable to the window dom object.
When coding the TypeScript isn’t complaining about the fact that gapi doesn’t exist on window object.
I’m trying to detect if the gapi file is loaded and if not then load it dynamically from my script.
GAPI doesn’t have a Type Definition that I can find so I’m trying to figure out how to check that it’s loaded into the DOM. I’ve been able to check it’s (non-) existence through the console on Chrome and through jQuery so either is an option. Just can’t figure out how to do this in TypeScript since it doesn’t know about gapi at all.
If you haven’t used the Google Analytics js library. It returns both a window.gapi and a gapi ref for you to use in code/DOM
To make TypeScript aware of the addition you would use:
Or the shorter:
You could optionally flesh out the definition.
This should then allow you to use gapi, including in your detection script: