Last active
August 15, 2017 23:45
-
-
Save cleicar/611741ec5f053727538565670a9f5637 to your computer and use it in GitHub Desktop.
JS Libary that send all user navigation to a SimpleCRM account.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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