(function ($) {
'use strict';
Drupal.behaviors.sidebarNewsletter = {
attach: function (context, settings) {
/**
* Sends event to Matomo.
*/
var sendEventToMatomo = function () {
if (!settings.effMatomoUrl) {
return;
}
$.post(settings.effMatomoUrl
.replace('e_a=', 'e_a=' + encodeURIComponent('Pending subscription created'))
.replace('e_c=', 'e_c=Subscription')
.replace('e_n=', 'e_n=' + encodeURIComponent('Sidebar newsletter'))
.replace('e_v=', 'e_v=1')
.replace('urlref=', 'urlref=' + encodeURIComponent(document.referrer))
);
};
/**
* Setup the subscribe form callback.
*/
$('form.newsletter-form', context).once('sidebar-newsletter-processed', function () {
$(this).submit(function (event) {
event.preventDefault();
var form = this;
$.post($(this).attr('action'), {
data_type: 'json',
email: $(this).find('input[name="email"]', this).val(),
form_id: $(this).find('input[name="form_id"]').val(),
op: $(this).find('input[name="op"]').val(),
postal_code: $(this).find('input[name="postal_code"]').val()
}).done(function (data) {
if (data.status) {
$(form).html("Thanks, you're awesome!
" + data.message);
sendEventToMatomo();
}
}).fail(function () {
$('.submitting', form).html('Oops something is broken right now, please try again later.');
$('input[name="op"]', form).show();
});
$('.submitting', form).show();
$('input[name="op"]', form).hide();
});
});
}
};
}(jQuery));