Skip to content

Instantly share code, notes, and snippets.

@cleicar
Last active August 15, 2017 23:45
Show Gist options
  • Save cleicar/611741ec5f053727538565670a9f5637 to your computer and use it in GitHub Desktop.
Save cleicar/611741ec5f053727538565670a9f5637 to your computer and use it in GitHub Desktop.
JS Libary that send all user navigation to a SimpleCRM account.
const SIMPLE_CRM_URL = 'https://my-simple-crm.herokuapp.com';
const USERNAME = 'cleiviane';
const PASSWORD = '12345678';
(function ($) {
var ready = $.fn.ready;
$.fn.ready = function (fn) {
addUserTrack();
$('#contact-form').submit(function(e) {
sendUserInfoToSimpleCRM();
e.preventDefault();
});
}
})(jQuery);
function addUserTrack(){
var visitedPage = window.location.href;
var storageId = 'visited_pages';
var storageValue = [visitedPage];
var value = null;
var savedTrack = getStorage(storageId);
if(savedTrack){
var pagesArray = savedTrack.split(',');
var exists = $.inArray(visitedPage, pagesArray);
if(exists == -1)
pagesArray.push(visitedPage)
storageValue = pagesArray;
}else{
storageValue = visitedPage
}
setStorage(storageId, storageValue);
}
function setStorage(name, value){
window.localStorage.setItem(name, value + ',');
}
function getStorage(name){
return window.localStorage.getItem(name);
}
function sendUserInfoToSimpleCRM() {
var contact = {
name: $('#name').val(),
email: $('#email').val(),
created_at: new Date(),
pages: getStorage('visited_pages')
}
$.ajax({
method: "POST",
url: SIMPLE_CRM_URL + "/api/contacts",
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(USERNAME + ":" + PASSWORD));
},
data: JSON.stringify({contact})
}).done(function() {
$("#result-message").show();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment