You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
amplitude.Types should be immediately defined after the browser SDK snippet is inserted. Example:
<scripttype="text/javascript">!function(){"use strict";!function(e,t){varr=e.amplitude||{_q:[],_iq:{}};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{varn=function(e,t){e.prototype[t]=function(){returnthis._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=function(e,t,r){returnfunction(n){e._q.push({name:t,args:Array.prototype.slice.call(r,0),resolve:n})}},o=function(e,t,r){e._q.push({name:t,args:Array.prototype.slice.call(r,0)})},i=function(e,t,r){e[t]=function(){if(r)return{promise:newPromise(s(e,t,Array.prototype.slice.call(arguments)))};o(e,t,Array.prototype.slice.call(arguments))}},a=function(e){for(vart=0;t<g.length;t++)i(e,g[t],!1);for(varr=0;r<m.length;r++)i(e,m[r],!0)};r.invoked=!0;varc=t.createElement("script");c.type="text/javascript",c.integrity="sha384-Dv01ylQWH3OMRpOnXgNXTEJypJSg/Si512H2KxfFik12c676LxjHoC1W2pF8xyR5",c.crossOrigin="anonymous",c.async=!0,c.src="https://cdn.amplitude.com/libs/analytics-browser-2.6.0-min.js.gz",c.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};varu=t.getElementsByTagName("script")[0];u.parentNode.insertBefore(c,u);for(varp=function(){returnthis._q=[],this},l=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],d=0;d<l.length;d++)n(p,l[d]);r.Identify=p;for(varf=function(){returnthis._q=[],this},v=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],y=0;y<v.length;y++)n(f,v[y]);r.Revenue=f;varg=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport","reset","extendSession"],m=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue","flush"];a(r),r.createInstance=function(e){returnr._iq[e]={_q:[]},a(r._iq[e]),r._iq[e]},e.amplitude=r}}(window,document)}();// eslint-disable-lineconsole.log(amplitude)// typeof amplitude.Types should not equal undefinedamplitude.init("<YOUR_API_KEY>");</script>
Current Behavior
amplitude.Types is undefined right below the browser SDK snippet and remains undefined until the Amplitude library injected by the snippet is loaded. My team discovered this when we tried to do something like:
Our code fails the first (and only the first) time this runs. This seems to be because the amplitude object properties are stubbed prior to the Amplitude library loading. However, the Types property does not appear to stubbed with the other expected properties.
On subsequent page loads, our code runs fine because the browser has cached the Amplitude library script (which loaded on the previous page load) and the amplitude object is fully hydrated immediately.
Possible Solution
Stub Types in amplitude-snippet.js so that the property is available before the library loads.
Steps to Reproduce
In a clean Chrome browser profile (it's important that you're in a window where the Amplitude library file hasn't been cached), add the following code:
<scripttype="text/javascript">!function(){"use strict";!function(e,t){varr=e.amplitude||{_q:[],_iq:{}};if(r.invoked)e.console&&console.error&&console.error("Amplitude snippet has been loaded.");else{varn=function(e,t){e.prototype[t]=function(){returnthis._q.push({name:t,args:Array.prototype.slice.call(arguments,0)}),this}},s=function(e,t,r){returnfunction(n){e._q.push({name:t,args:Array.prototype.slice.call(r,0),resolve:n})}},o=function(e,t,r){e._q.push({name:t,args:Array.prototype.slice.call(r,0)})},i=function(e,t,r){e[t]=function(){if(r)return{promise:newPromise(s(e,t,Array.prototype.slice.call(arguments)))};o(e,t,Array.prototype.slice.call(arguments))}},a=function(e){for(vart=0;t<g.length;t++)i(e,g[t],!1);for(varr=0;r<m.length;r++)i(e,m[r],!0)};r.invoked=!0;varc=t.createElement("script");c.type="text/javascript",c.integrity="sha384-Dv01ylQWH3OMRpOnXgNXTEJypJSg/Si512H2KxfFik12c676LxjHoC1W2pF8xyR5",c.crossOrigin="anonymous",c.async=!0,c.src="https://cdn.amplitude.com/libs/analytics-browser-2.6.0-min.js.gz",c.onload=function(){e.amplitude.runQueuedFunctions||console.log("[Amplitude] Error: could not load SDK")};varu=t.getElementsByTagName("script")[0];u.parentNode.insertBefore(c,u);for(varp=function(){returnthis._q=[],this},l=["add","append","clearAll","prepend","set","setOnce","unset","preInsert","postInsert","remove","getUserProperties"],d=0;d<l.length;d++)n(p,l[d]);r.Identify=p;for(varf=function(){returnthis._q=[],this},v=["getEventProperties","setProductId","setQuantity","setPrice","setRevenue","setRevenueType","setEventProperties"],y=0;y<v.length;y++)n(f,v[y]);r.Revenue=f;varg=["getDeviceId","setDeviceId","getSessionId","setSessionId","getUserId","setUserId","setOptOut","setTransport","reset","extendSession"],m=["init","add","remove","track","logEvent","identify","groupIdentify","setGroup","revenue","flush"];a(r),r.createInstance=function(e){returnr._iq[e]={_q:[]},a(r._iq[e]),r._iq[e]},e.amplitude=r}}(window,document)}();// eslint-disable-lineconsole.log(amplitude);debugger;amplitude.init("<YOUR_API_KEY>");</script>
When debugger pauses script execution, take a look at the amplitude object in the console. You should see something like:
Notice that Types is not defined.
Wait a few seconds and/or refresh your page, and type amplitude into the devtools console. Types should now be defined on the amplitude object.
Expected Behavior
amplitude.Types
should be immediately defined after the browser SDK snippet is inserted. Example:Current Behavior
amplitude.Types
isundefined
right below the browser SDK snippet and remainsundefined
until the Amplitude library injected by the snippet is loaded. My team discovered this when we tried to do something like:Our code fails the first (and only the first) time this runs. This seems to be because the
amplitude
object properties are stubbed prior to the Amplitude library loading. However, theTypes
property does not appear to stubbed with the other expected properties.On subsequent page loads, our code runs fine because the browser has cached the Amplitude library script (which loaded on the previous page load) and the
amplitude
object is fully hydrated immediately.Possible Solution
Stub
Types
inamplitude-snippet.js
so that the property is available before the library loads.Steps to Reproduce
debugger
pauses script execution, take a look at theamplitude
object in the console. You should see something like:Notice that
Types
is not defined.amplitude
into the devtools console.Types
should now be defined on theamplitude
object.Environment
The text was updated successfully, but these errors were encountered: