Skip to content

Commit

Permalink
Stop the webchat events firing on every ajax check
Browse files Browse the repository at this point in the history
previous functionality was to send analytics on every state change,
this now only sends state changes to analytics as opposed to every
time we poll.

it alos includes lolex so we can do fake timers to test this,
recommendation from @NickColley
  • Loading branch information
Andrew Hilton committed Apr 10, 2017
1 parent cbc6f92 commit 32b589a
Show file tree
Hide file tree
Showing 4 changed files with 717 additions and 4 deletions.
11 changes: 10 additions & 1 deletion app/assets/javascripts/webchat/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
var webchatStateClass = 'js-webchat-advisers-'
var responseNormaliser = options.responseNormaliser
var intervalID = null
var lastRecordedState = null

function init () {
if (!availabilityUrl || !openUrl) throw 'urls for webchat not defined'
Expand All @@ -33,7 +34,7 @@
function handleOpenChat (evt) {
evt.preventDefault()
global.open(openUrl, 'newwin', 'width=200,height=100')
GOVUK.analytics.trackEvent('webchat', 'opened')
trackEvent('opened')
}

function checkAvailability () {
Expand Down Expand Up @@ -65,7 +66,15 @@
var currentState = $el.find("." + webchatStateClass + state)
$el.find('[class^="' + webchatStateClass + '"]').addClass('hidden')
currentState.removeClass('hidden')
trackEvent(state)
}

function trackEvent (state) {
state = state.toLowerCase()
if (lastRecordedState === state) return

GOVUK.analytics.trackEvent('webchat', state)
lastRecordedState = state
}

init()
Expand Down
Loading

0 comments on commit 32b589a

Please sign in to comment.