Using the Freshdesk/Freshdesk Widget with Turbolinks
Hello,
I’ve been using the Freshdesk widget on my production app for a while now. I want to be able to pre-fill a user's email address as well as automatically set the ticket priority for my paid users to "High".
I've got it set up such that on the initial website loads or page refreshes, everything works as is expected, but when navigating from page-to-page within the app, only the widget is initialised, but none of the extra stuff is set up.
Here's how my code is set up.
In app/views/layouts/application.html.erb
, just before the closing </body>
tag, I have:
<%= render "layouts/freshdesk_widget" %>
Inside app/views/layouts/_freshdesk_widget.html.erb
, I have the following:
<script>
window.fwSettings={
'widget_id':<widget_id>
};
!function(){if("function"!=typeof window.FreshworksWidget){var n=function(){n.q.push(arguments)};n.q=[],window.FreshworksWidget=n}}()
</script>
<script type='text/javascript' src='https://euc-widget.freshworks.com/widgets/<widget_id>.js' async defer></script>
<script type='text/javascript'>
FreshworksWidget('identify', 'ticketForm', { email: '<%= "#{current_user.try(:email)}" %>' });
FreshworksWidget('hide', 'ticketForm', ['priority']);
FreshworksWidget('prefill', 'ticketForm', { priority: <%= current_user.try(:paid?) ? 3 : 1 %> });
</script>
I’ve tried many different things, from trying to call the code in different places, to trying to manually do what I want, but to no avail.
Ever since I started using this widget, when navigating to other pages after the initial page load, I always get the following error in my console:
VM2797 <widget_id>.js:1 Uncaught TypeError: Cannot read property 'postMessage' of null
at Object.postMessage (VM2797 <widget_id>.js:1)
at Object.widgetRenderComplete (VM2797 <widget_id>.js:1)
at Object.handleMessage (VM2797 <widget_id>.js:1)
I did get rid of these errors by putting the embed code in the <head>
of application.html.erb
, but it meant the widget didn’t load again beyond the initial page load. So, I put everything back to where the documentation said it should go and have just been ignoring the error message. I wonder whether it has any bearing on the problems I’m having now.
Thank you in advance for any help I get! Let me know if you have any more questions or need to see more parts of the code.
P.S. I know it's Turbolinks causing the issue because when I disable it, everything works as expected. However, I heavily rely on Turbolinks in other parts of the application!