0

JavaScript noob here. I am using a Piwik script and adding a bit to it.

I'd like to get access to "visitor_id" outside of it's scope and use it globally. Currently it's returning "undefined", using alert outside of it's scope.

<script type="text/javascript">
    var visitor_id;
    var _paq = _paq || [];
    visitor_id = _paq.push(['this.getVisitorId()']);
    _paq.push(['trackPageView']);
    _paq.push(['enableLinkTracking']);
    (function() {
    var u="//stats.agahin.com/";
    _paq.push(['setTrackerUrl', u+'agahin.php']);
    _paq.push(['setSiteId', 1]);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'agahin.js'; s.parentNode.insertBefore(g,s);
    })();
    _paq.push([ function () {
        visitor_id = this.getVisitorId();
        /*
        if (!visitor_id){
            document.getElementById('visitorId').value = visitor_id;
            if (document.getElementById('visitorId').value != 1) {
                document.getElementById("visitorForm").submit();
            };
        };
        */
    }]);
    alert(visitor_id);
</script>
Hossein J
  • 1,019
  • 1
  • 15
  • 32
  • 2
    `visitor_id` is already declared as a global variable, it's the first line in your script block. It's undefined because you're alerting it before the callback function has executed – Andy Oct 11 '15 at 12:55
  • 1
    Dammit, I didnt intent to single handled close your question. stupid gold star. But as you can see the problem is most likely that your function is not called when you try to access visitor_id – Andreas Louv Oct 11 '15 at 12:57
  • I don't know how to implement this. I tried the example in the duplicate but still no success. – Hossein J Oct 11 '15 at 13:08

0 Answers0