-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdemandbaseGoogleAnalyticsIntegrationSample.html
More file actions
123 lines (113 loc) · 6.09 KB
/
demandbaseGoogleAnalyticsIntegrationSample.html
File metadata and controls
123 lines (113 loc) · 6.09 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<!--
Name: Demandbase Google Analytics Integration Sample
Authors: Ilya Hoffman (Ilya[at@]SynapseAutomation[dot.]com), Matthew Downs (mdowns[at]demandbase[dot]com)
License: Copyright 2012. This code may not be reused without explicit permission by its owners. All rights reserved.
Contact Demandbase Strategic Services with questions about using this code.
This script can be generated automatically with your desired custom variables using the online script generator is available at:
www.demandbaselabs.com/ga
-->
<!-- Start Asynchronous Solution (Recommended!) -->
<!-- Standard Google Analytics Integration (no changes required if GA is already on page)-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '[YOUR_GA_PROPERTY_ID]']);
_gaq.push(['_setDomainName', '[YOUR_DOMAIN]']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- Demandbase/GA Integration -->
<!-- This script works along side your current GA integration. Place this script at the END of the HTML head -->
<script type="text/javascript">
var DemandbaseAnalytics=DemandbaseAnalytics||{};
DemandbaseAnalytics.demandbaseGA={
key : '[YOUR_DEMANDBASE_KEY]', //TODO: add your Demandbase key
/* Customize which variables are sent to GA. 'fields' must be a Demandbase variable name */
fields : ['audience','industry','company_name','employee_range','revenue_range'], //TODO: choose Demandbase fields to send to GA
dbDataSet: null,
track:function(data) {
try {
data=DemandbaseAnalytics.demandbaseGA._flatA(data);
DemandbaseAnalytics.demandbaseGA.dbDataSet = data; /*allows access to db dataset in JS outside of this namespace */
for(field in DemandbaseAnalytics.demandbaseGA.fields){
var lbl=DemandbaseAnalytics.demandbaseGA._cA(DemandbaseAnalytics.demandbaseGA._toPC(DemandbaseAnalytics.demandbaseGA.fields[field]));
DemandbaseAnalytics.demandbaseGA._var((parseInt(field)+1),lbl,(data[DemandbaseAnalytics.demandbaseGA.fields[field]]||''),1);
DemandbaseAnalytics.demandbaseGA._logE(lbl + ' : ' + data[DemandbaseAnalytics.demandbaseGA.fields[field]]||'');
}
DemandbaseAnalytics.demandbaseGA._event();
} catch(e){ DemandbaseAnalytics.demandbaseGA._logE('Integration Error: '+e)};
},
load:function(){
try {
if(!window._gaq) window._gaq=[];
var db=document.createElement('script');db.type='text/javascript'; db.async=true;
db.src=('https:'==document.location.protocol?'https://':'http://')+'api.demandbase.com/api/v2/ip.json?key='+this.key+'&callback=DemandbaseAnalytics.demandbaseGA.track&page='+document.URL+'&referrer='+document.referrer;
var s=document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(db,s);
_gaq.push(['_addDevId', 'NE7T9']);
DemandbaseAnalytics.demandbaseGA._logE('Loaded Script ' + db.src);
} catch(e){ this._logE('Script Error: '+e)};
},
_cA:function(s){
var al=['IP','SID','HQ','DMA','ISP'];
for(a in al){
s=s.replace(new RegExp(al[a],'ig'),al[a])}return s;
},
_flatA:function(a){
for(d in a){
if(typeof a[d]=='object'){
for(cd in a[d]){a[d+'_'+cd]=a[d][cd]};
delete a[d];
}
};
return a;
},
_logE:function(m){ if(window['console'] !== 'undefined' && typeof(console) !== 'undefined') console.log('DB GA '+m); },
_toPC:function(s){
return s.replace(/_/g,' ').replace(/\w\S*/g,function(t){ return t.charAt(0).toUpperCase()+t.substr(1).toLowerCase() }); },
_p:function(t,v1,v2,v3,v4,v5){ window._gaq.push([t,v1,v2,v3,v4,v5]); },
_var:function(i,k,v,s){ this._p('_setCustomVar',i,k,v,s); },
_event:function(){ this._p('_trackEvent','Demandbase','API Resolution','IP API',0,1); }
};
DemandbaseAnalytics.demandbaseGA.load();
</script>
<!-- End Demandbase/GA Integration -->
<!-- End Asynchronous Solution -->
<!-- ************************************************************** -->
<!-- Start Synchronous Solution (Only Use for Special Cases!)
Replaces the current GA script on your page.
Talk to your Demandbase representative before using this script!
-->
<!-- place this script in the <head> of the page -->
<script type="text/javascript" src="http://api.demandbase.com/api/v2/ip.js?key=[YOUR_DEMANDBASE_KEY]"></script>
<!-- place this script at the end of the <body> of the page -->
<script type="text/javascript">
//Note 'organization' is set by Demandbase API call above. Use var parameter to override variable name, if neeeded
function formatCV(name){return organization[name]?organization[name]:''};
var _gaq=_gaq||[];
_gaq.push(['_setAccount','[YOUR_GA_PROPERTY_ID]']);
if(typeof organization!='undefined'){
//TODO: Update with field labels and Demandbase variable names to send to GA
//Note: Remove any lines for custom variables that are already used
_gaq.push(['_setCustomVar', 1, '[FIELD_NAME1]', formatCV('[DEMANDBASE_VARIABLE_NAME1]'), 1]);
_gaq.push(['_setCustomVar', 2, '[FIELD_NAME2]', formatCV('[DEMANDBASE_VARIABLE_NAME2]'), 1]);
_gaq.push(['_setCustomVar', 3, '[FIELD_NAME3]', formatCV('[DEMANDBASE_VARIABLE_NAME3]'), 1]);
_gaq.push(['_setCustomVar', 4, '[FIELD_NAME4]', formatCV('[DEMANDBASE_VARIABLE_NAME4]'), 1]);
_gaq.push(['_setCustomVar', 5, '[FIELD_NAME5]', formatCV('[DEMANDBASE_VARIABLE_NAME5]'), 1]);
}
_gaq.push(['_trackPageview']);
(function(){
var ga=document.createElement('script');
ga.type='text/javascript';
ga.async=true;
ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js';
var s=document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga,s);
})();
</script>
<!-- End Synchronous Solution -->