[SalesForce] Capture URL of webpage containing Live Agent chat button

We are implementing Live Agent with a pre-chat form to capture and display information from the customer. The pre-chat form works great but we also want capture the URL the customer was on when they clicked the chat button.

I added some JavaScript to pass in this detail from the webpage.

liveagent.addCustomDetail('URL', window.location.href);

This works fine from a test HTML page on my desktop, but when we put the code on our testing website the URL doesn't get passed through.

On the webpage the code is in the <body> tag and looks like this.

<script type="text/javascript">
    if (!window._laq) { window._laq = []; }
    window._laq.push(function(){
        liveagent.showWhenOnline('573XXXXXXXXXXXX', document.getElementById('liveagent_button_online_573XXXXXXXXXXXX'));
        liveagent.showWhenOffline('573XXXXXXXXXXXX', document.getElementById('liveagent_button_offline_573XXXXXXXXXXXX'));
    });
</script>
<script type='text/javascript' src='https://c.la3-c2cs-chi.salesforceliveagent.com/content/g/js/37.0/deployment.js'></script>

<script>
    // liveagent.addCustomDetail('URL', window.location.href); Doesn't work either
    window.onload=liveagent.addCustomDetail('URL', window.location.href);
</script>

<script type='text/javascript'>
    liveagent.init('https://d.la3-c2cs-chi.salesforceliveagent.com/chat', '572XXXXXXXXXXXX', '00DXXXXXXXXXXXX');
</script>

I'm not JavaScript wiz so I'm not sure if/how much placement matters.

Also using FireFox firebug I see this error when I refresh the page

ReferenceError: liveagent is not defined

I suspect this is caused by the addCustomDetail function but I'm not sure exactly how to find exactly where it is happening.

enter image description here

Best Answer

So I managed to solve my problem with the following bit of JavaScript code.

<script>
    document.addEventListener
    (
        'DOMContentLoaded',
        function(event)
        {
            liveagent.addCustomDetail('URL', window.location.href);
        }
    );
</script>

That captured the URL and passed it