is your analytics / AppMeasurement "s" instance globally accessible?

The problem you are likely facing is that throwing the function in the Analytics custom code will not necessarily attach it to the "s" object.
https://experienceleague.adobe.com/en/docs/analytics/implementation/vars/plugins/getvisitnum
If you take the code from there, you can see

Since you are trying to access the function from a custom code javascript data element, this will be unreachable unless
a) you define it as function on the "s" object OR
s.getVisitNum = function(rp,erp){...
and call is through s.getVisitNum()
b) define it as window scoped function (this case does not need a globally accesible "s" tracker object)
window.getVisitNum = function(rp,erp){...
and call it through window.getVisitNum()
If you really do not want to use the extension (which I would recommend), I would likely go for option b)