docbrown/app/assets/javascripts/initializers/initializeBaseTracking.js.erb
Mac Siri 40f488d991 Migrate to Travis CI (part 2) (#591)
* Update Travis.yml

* Update README

* Update README

* Adjust test to not rely on env vars

* Update encryption

* Refactor

* Update env key

* Stub AWS calls

* Create ApplicationConfig

* Fix specs

* Fix lint

* Update ApplicationConfig

* Remove travis env vars

* Fix lint

* Extend character limit to 100

* Add env to travis

* Take out auto-restart after deploy

* Immediately discarded test cache

* Stub GA in request specs

* Stub Pusher

* Fix broken specs

* Update fixture

* Add CodeClimate id

* Change CodeClimate key

* Remove merge mistakes

* WIP

* Add Envied gem & Change README

* Add missing keys

* Add missing key

* Update fixture

* Fix broken spec

* Add Slack Notification for Travis

* Fix wording

* Fix typo
2018-07-20 20:17:18 -04:00

100 lines
No EOL
3.4 KiB
Text

function initializeBaseTracking() {
var wait = 0;
var addedGA = false;
var waitingOnGA = setInterval(function() {
if (!addedGA) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
}
addedGA = true;
wait++;
if (window.ga && ga.create) {
ga('create', '<%= ApplicationConfig["GA_TRACKING_ID"] %>', 'auto');
if (!checkUserLoggedIn() && Math.random() < 0.35) {
ga('require', '<%= ApplicationConfig["GA_OPTIMIZE_ID"] %>');
}
ga('send', 'pageview', location.pathname + location.search);
if (document.location.search.indexOf('newly-registered-user=true') > -1) {
ga('send', 'event', 'registration', 'New user registration', location.pathname, null);
}
clearInterval(waitingOnGA);
}
if (wait > 85) {
clearInterval(waitingOnGA);
fallbackActivityRecording();
}
}, 25);
facebookTrackingScript();
eventListening();
}
function fallbackActivityRecording() {
var tokenMeta = document.querySelector("meta[name='csrf-token']")
if (!tokenMeta) {
return
}
var csrfToken = tokenMeta.getAttribute('content')
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var screenW = window.screen.availWidth;
var screenH = window.screen.availHeight;
var dataBody = {
path: location.pathname + location.search,
user_language: navigator.language,
referrer: document.referrer,
user_agent: navigator.userAgent,
viewport_size: h + 'x' + w,
screen_resolution: screenH + 'x' + screenW,
document_title: document.title,
document_encoding: document.characterSet,
document_path: location.pathname + location.search,
};
window.fetch('/fallback_activity_recorder', {
method: 'POST',
headers: {
Accept: 'application/json',
'X-CSRF-Token': csrfToken,
},
body: JSON.stringify(dataBody),
credentials: 'same-origin'
});
}
function fbTrack() {
fbq('track', 'PageView');
if (document.location.search.indexOf('newly-registered-user=true') > -1) {
fbq('track', 'CompleteRegistration');
history.replaceState(null, document.title, window.location.href.replace('?newly-registered-user=true', ''));
}
}
function facebookTrackingScript() {
if (typeof fbq === 'undefined') {
// Also don't lint this minified function
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '<%= ApplicationConfig["FACEBOOK_PIXEL_ID"] %>');
fbTrack();
} else {
fbTrack();
}
}
function eventListening(){
var registerNowButt = document.getElementById("cta-comment-register-now-link");
if (registerNowButt) {
registerNowButt.onclick = function(){
ga('send', 'event', 'click', 'register-now-click', null, null);
}
}
}