Accessing variables from custom code script

Hello,

I am trying to use a global variable that is created by a script added to in the head of the page in the ‘Custom Code’ settings. I know this script is working correctly as on the published public site, I can use the variable within the developer console. However, I can not get this variable to work when using an onClick event handler within Velo, inside masterPage.js. When a test site is published, the click creates a console error stating that the variable is undefined.

Is there a way I can ensure the script is ran before the event handler context so that it has access to the variable I need?

Where is your already generated code ?

I added a script tag to the head using Settings > Custom Code > Add Custom Code. It is set to be added to all pages and as essential purpose.