-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.js
83 lines (70 loc) · 3.2 KB
/
connector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var url = "https://www.google-analytics.com/collect"
//var gaId = Your Google Analytics ID here, it begins with UA-;
var version = 1;
var clientId = 5;
var hitType = "event";
//Function to send data to Google Analytics via measurement protocol POST request
function send(payload) {
var http = new XMLHttpRequest();
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
try {
http.send(encodeURI(payload));
}
catch(err) {
alert("error: " + err.message);
}
}
//Function to create payload when task is created
function Activity() {
var subjectField = "tsk5";
var statusField = "tsk12";
var eventCategory = "activity";
var subject = window.document.getElementById(subjectField).value;
var status = window.document.getElementById(statusField).value;
var params = "v=" + version + "&tid=" + gaId + "&cid=" + clientId + "&t=" + hitType + "&ec=" + eventCategory + "&ea=" + subject + "&el=" + status;
send(params);
}
//Function to create payload when lead status changed
function LeadStatus() {
var statusField = "lea13_ileinner";
var newLeadStatus = window.document.getElementById(statusField).textContent;
var eventCategory = "lead-status-change";
if (leadStatus != newLeadStatus) {
var params = "v=" + version + "&tid=" + gaId + "&cid=" + clientId + "&t=" + hitType + "&ec=" + eventCategory + "&ea=" + newLeadStatus + " from " + leadStatus;
send(params);
}
}
//Function to create payload when opportunity stage changed
function OpportunityStatus() {
var statusField = "opp11_ileinner";
var revenueField = "opp7_ileinner";
var newOpportunityStatus = window.document.getElementById(statusField).textContent;
var eventCategory = "opportunity-status-change";
if (opportunityStatus != newOpportunityStatus) {
var params = "v=" + version + "&tid=" + gaId + "&cid=" + clientId + "&t=" + hitType + "&ec=" + eventCategory + "&ea=" + newOpportunityStatus + " from " + opportunityStatus;
if (newOpportunityStatus == "Closed Won") {
var revenue = window.document.getElementById(revenueField).textContent;
var revenueStripped = parseInt(revenue.replace( /[^\d.]/g, '' ));
params += ("&ev=" + revenueStripped);
}
send(params);
}
}
//Function to apply event listeners to appropriate elements depending on task, lead status change or opportunity stage change
function ApplyEventListeners(elementId, callbackFunction) {
saveButtons = window.document.getElementsByName(elementId);
for (var i = 0; i < saveButtons.length; i++) {
saveButtons[i].addEventListener("click", callbackFunction, false);
}
}
//Check for task, lead status change or opportunity stage change
if (window.location.href.indexOf("/00T/") != -1) {
ApplyEventListeners("save", Activity);
} else if (window.location.href.indexOf("/00Q") != -1) {
ApplyEventListeners("inlineEditSave", LeadStatus);
var leadStatus = window.document.getElementById("lea13_ileinner").textContent;
} else if (window.location.href.indexOf("/006") != -1) {
ApplyEventListeners("inlineEditSave", OpportunityStatus);
var opportunityStatus = window.document.getElementById("opp11_ileinner").textContent;
}