Attempting to send data to Amplitude, playcanvas is throwing error during parsing script

hello, I’m trying to use a script that allows me to track data via Amplitude. I’m attempting to do this using the instructions given on the Amplitude website, which gives the following code to put into the script:

(function(e,t){var n=e.amplitude||{_q:[],_iq:{}};var r=t.createElement("script")
  ;r.type="text/javascript"
  ;r.integrity="sha384-+EO59vL/X7v6VE2s6/F4HxfHlK0nDUVWKVg8K9oUlvffAeeaShVBmbORTC2D3UF+"
  ;r.crossOrigin="anonymous";r.async=true
  ;r.src="https://cdn.amplitude.com/libs/amplitude-8.17.0-min.gz.js"
  ;r.onload=function(){if(!e.amplitude.runQueuedFunctions){
  console.log("[Amplitude] Error: could not load SDK")}}
  ;var i=t.getElementsByTagName("script")[0];i.parentNode.insertBefore(r,i)
  ;function s(e,t){e.prototype[t]=function(){
  this._q.push([t].concat(Array.prototype.slice.call(arguments,0)));return this}}
  var o=function(){this._q=[];return this}
  ;var a=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove"]
  ;for(var c=0;c<a.length;c++){s(o,a[c])}n.Identify=o;var u=function(){this._q=[]
  ;return this}
  ;var l=["setProductId","setQuantity","setPrice","setRevenueType","setEventProperties"]
  ;for(var p=0;p<l.length;p++){s(u,l[p])}n.Revenue=u
  ;var d=["init","logEvent","logRevenue","setUserId","setUserProperties","setOptOut","setVersionName","setDomain","setDeviceId","enableTracking","setGlobalUserProperties","identify","clearUserProperties","setGroup","logRevenueV2","regenerateDeviceId","groupIdentify","onInit","logEventWithTimestamp","logEventWithGroups","setSessionId","resetSessionId"]
  ;function v(e){function t(t){e[t]=function(){
  e._q.push([t].concat(Array.prototype.slice.call(arguments,0)))}}
  for(var n=0;n<d.length;n++){t(d[n])}}v(n);n.getInstance=function(e){
  e=(!e||e.length===0?"$default_instance":e).toLowerCase()
  ;if(!Object.prototype.hasOwnProperty.call(n._iq,e)){n._iq[e]={_q:[]};v(n._iq[e])
  }return n._iq[e]};e.amplitude=n})(window,document);

When parsing the script in the editor, the following error occurs:
Uncaught ReferenceError: document is not defined

I have verified that this code does work in other contexts without any modifications, so I was wondering if playcanvas has an option to disable error checking on this script to allow it to run? or if there are any modifications that need to be done in order to get it working?

Can you link to the documentation from Amplitude please? Or even better, a project that shows this issue please?

here’s an example project with the script showing the error:
https://playcanvas.com/editor/scene/1537571

here’s the documentation for amplitude:

Hello, and welcome to PlayCanvas!

So, parsing a script in Editor is only intended for pc.ScriptType type of scripts, to sync any potential script attributes with Editor or update the script name in the database, etc.

For any other scripts, you could just create a separate script, remove default template code and paste the amplitude contents there. No need parsing it. In project settings make sure it is loaded before the pc.ScriptType that is using it.

Ah, that doesn’t need to be in a script that requires parsing.

https://playcanvas.com/project/985076/overview/f-amplitude-script-bug-demo

Ignore the error when it tries to parse the script as it doesn’t have a ScriptType in it or you can workaround it by checking if window.document exists

Thank you for the help! I will give it a go, the reason I was originally parsing the script is so that I could have easy access to change the API Key along with a modifiable array in the editor panel of the interactions that I wanted to track, but if it is unable to be parced in that way then I will find a different method of doing this without having to parce that block of code, thank you! I’ll get back to you if I run into any other issues with it

In which case, put that Amplitude code into the initialise function of a ScriptType that has attributes for the properties you wanted. It would mean that the code will be executed later because initialise has to be called thought. See Application Lifecycle | Learn PlayCanvas

1 Like

I’ve noticed that you can put the code in the initialize and it won’t flag any errors in the editor, however any changes to Script Attributes will not get picked up in the editor

1 Like