Skip to content

Instantly share code, notes, and snippets.

@reyaz
Last active December 16, 2015 23:30
Show Gist options
  • Save reyaz/ad7f21dd19d043f577c2 to your computer and use it in GitHub Desktop.
Save reyaz/ad7f21dd19d043f577c2 to your computer and use it in GitHub Desktop.
Desk.com Widget URL Builder for OpenSky.com Contact Page
/* This script passes data from OpenSky.com's main JS object to Desk.com via
* query params for a customized contact form loaded in an iframe (yuck). */
/* Hide the iframe (if it’s not already hidden) */
$(document.getElementById('desk-contact-iframe')).addClass('is-hidden');
// http://www.joezimjs.com/javascript/3-ways-to-parse-a-query-string-in-a-url/
var parseQueryString = function( queryString ) {
var params = {}, queries, temp, i, l;
// Split into key/value pairs
queries = queryString.split("&");
// Convert the array of strings into an object
for ( i = 0, l = queries.length; i < l; i++ ) {
temp = queries[i].split('=');
params[temp[0]] = temp[1];
}
return params;
};
/**
* Builds the URL for the Desk Widget iframe
*
* @param {object} fields The data to be passed to Desk.com.
* @returns {string} The URL containing that data.
*/
var parseDeskWidgetSrc = function(fields) {
var host = "opensky.desk.com",
path = "/customer/widget/emails/new",
protocol = window.location.protocol + '//',
search = "";
for (var group in fields) {
for (var param in fields[group]) {
if (escape(fields[group][param]).length > 0) {
var queryParam = "%group%[%param%]=%value%";
queryParam = queryParam.replace("%group%", escape(group));
queryParam = queryParam.replace("%param%", escape(param));
queryParam = queryParam.replace("%value%", escape(fields[group][param]));
if (search.length > 0) {
search = [search, queryParam].join('&');
} else {
search = '?' + queryParam;
};
}
}
};
return protocol + host + path + search;
};
var queryParams = parseQueryString(window.location.search.substring(1));
var memberTypes = {
'superLucy': 'Super Lucy',
'buyer': 'Buyer',
'guest_buyer': 'Guest Buyer',
'non_buyer': 'Non Buyer',
'guest_non_buyer': 'Guest Non Buyer',
'repeat': 'Repeat',
'guest_repeat': 'Guest Repeat',
'network': 'Network Buyer',
'guest_network': 'Guest Network',
'repeatNetwork': 'Repeat Network Buyer',
'guest_repeatNetwork': 'Guest Repeat Network Buyer'
};
var memberTypeLabel = memberTypes[Osky.User['memberType']] || '';
var deskWidgetFields = {
customer: {},
email: {},
interaction: {
email: Osky.User.email || '',
name: Osky.User.fullname || ''
},
ticket: {
custom_order_id: queryParams['orderId'] || queryParams['order'] || '',
custom_osky_user_email: Osky.User.email || '',
custom_osky_user_full_name: Osky.User.fullname || '',
custom_osky_user_id: Osky.User.id || '',
custom_osky_user_slug: Osky.User.slug || '',
custom_osky_user_supplier_id: Osky.User.supplierId || '',
labels_new: [memberTypeLabel]
}
};
if (Osky.User.isMerchant) {
deskWidgetFields.ticket.labels_new.push('Merchant');
};
if (Osky.User.isCoPilotActive) {
deskWidgetFields.ticket.labels_new.push('CoPilot Merchant Active');
deskWidgetFields.ticket.priority = 9;
};
if (Osky.User.isCoPilotAds) {
deskWidgetFields.ticket.labels_new.push('CoPilot Merchant Ads');
};
if (Osky.User.isCoPilotSuspended) {
deskWidgetFields.ticket.labels_new.push('CoPilot Merchant Suspended');
};
/* Parse & set the iframe src */
document.getElementById('desk-contact-iframe').src = parseDeskWidgetSrc(deskWidgetFields);
/* Unhide the iframe (and hide some other elements) */
$(document.getElementById('contact-us-loading')).addClass('is-hidden');
$(document.getElementById('contact-us-email-link')).addClass('is-hidden');
$(document.getElementById('desk-contact-iframe')).removeClass('is-hidden');
@reyaz
Copy link
Author

reyaz commented Apr 17, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment