Skip to content

Instantly share code, notes, and snippets.

@czenzel
Last active September 27, 2019 11:50
Show Gist options
  • Save czenzel/101eee9685e7e40f1722685baaa39319 to your computer and use it in GitHub Desktop.
Save czenzel/101eee9685e7e40f1722685baaa39319 to your computer and use it in GitHub Desktop.
Christopher Zenzel Analytics Plug-In
/*
Christopher Zenzel Analytics
Advanced Analytics for Google Analytics Platform
Copyright 2016 Christopher David Zenzel
All Rights Reserved.
https://chriszenzel.com
For the purpose of this project and why certain
data is collected please visit my web site.
*/
/*
Advanced Analytics Categories
*/
var adv_analytics_category = 'Clients';
var adv_analytics_category_links = 'Links';
var adv_analytics_category_forms = 'Contact Forms';
var adv_analytics_category_yt = 'YouTube';
var adv_analytics_category_video5 = 'HTML 5 Video';
/*
Advanced Analytics Data
*/
var adv_analytics_ip = 0;
/*
Google Analytics Advanced Analytics and Reporting
for IP Address, ISP Values, and Harassment/Abuse Tracking
*/
$(document).ready(function() {
/*
Only allow analytics if Google Analytics is present
*/
if (typeof ga != 'undefined') {
/*
Get IP Address Information
*/
$.ajax({
dataType: 'json',
url: 'https://api.ipify.org?format=json',
success: function (ip_data) {
var ip_address = ip_data.ip;
adv_analytics_send('IP Address', ip_address);
/*
Store the IP Address for the Contact Form
*/
adv_analytics_ip = ip_address;
/*
Get ISP and IP Lookup Information
once we have an IP address
*/
$.ajax({
dataType: 'json',
url: 'https://whois.arin.net/rest/ip/' + encodeURIComponent(ip_address) + '.json',
success: function (arin_data) {
var network = arin_data.net;
if (network.orgRef) {
var orgName = network.orgRef['@name'];
adv_analytics_send('ISP Organization', orgName);
}
if (network.customerRef) {
var custName = network.customerRef['@name'];
adv_analytics_send('ISP Customer Name', custName);
}
}
});
}
});
}
});
/*
Google Analytics for Outbound Links
*/
$(document).ready(function() {
if (typeof ga != 'undefined') {
$('a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).on('click', function() {
var url = this.href;
adv_analytics_links_send(url);
});
}
});
/*
Google Analytics for Contact Forms
*/
$(document).ready(function() {
if (typeof ga != 'undefined') {
$('form a.wsite-button').click(function() {
if (adv_analytics_ip && adv_analytics_ip != 0) {
adv_analytics_forms_send('Submission IP Address', adv_analytics_ip);
}
adv_analytics_forms_send('Submission Status', 'Submitted');
});
}
});
/*
Send Google Analytics Event for Contact Form
*/
function adv_analytics_forms_send (received_action, received_value) {
if (typeof ga != 'undefined') {
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_forms,
eventAction: received_action,
eventLabel: received_value,
nonInteraction: true
});
}
}
/*
Send Google Analytics Event for Hyperlinks
*/
function adv_analytics_links_send (received_value) {
if (typeof ga != 'undefined') {
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_links,
eventAction: 'External Link',
eventLabel: received_value,
transport: 'beacon',
nonInteraction: true
});
}
}
/*
Send Google Analytics Event
*/
function adv_analytics_send (received_action, received_value) {
if (typeof ga != 'undefined') {
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category,
eventAction: received_action,
eventLabel: received_value,
nonInteraction: true
});
}
}
/*
General Video Tracking
*/
var adv_analytics_video_timer = 10;
/*
YouTube Video Tracking
Work in Progress
*/
$(document).ready(function() {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function isYouTubeVideo (videoEmbed) {
var embedSrc = videoEmbed.src || '';
if (embedSrc.indexOf('youtube.com/embed/') > -1 ||
embedSrc.indexOf('youtube.com/v/') > -1) {
return true;
}
return false;
}
var iframes = document.getElementsByTagName('iframe');
$(document).arrive('iframe', function() {
var iframe = $(this).get(0);
var videoTimer = 0;
var videoPlayLength = 0;
function onYouTubeIframeAPIReady(event) {
var player = new YT.Player('youTubePlayer', {
events: {
'onStateChange': function(event) {
if (event.data == YT.PlayerState.PLAYING) {
videoTimer = setInterval(function() {
videoPlayLength += adv_analytics_video_timer;
console.log('User watched a video for ' + videoPlayLength + ' seconds');
if (videoPlayLength > 0) {
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_yt,
eventAction: 'Time Played',
eventLabel: player.getVideoData().title,
eventValue: videoPlayLength,
nonInteraction: true
});
}
}, 1000 * adv_analytics_video_timer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_yt,
eventAction: 'Play',
eventLabel: player.getVideoData().title,
nonInteraction: true
});
}
if (event.data == YT.PlayerState.PAUSED) {
clearInterval(videoTimer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_yt,
eventAction: 'Paused',
eventLabel: player.getVideoData().title,
nonInteraction: true
});
}
if (event.data == YT.PlayerState.ENDED) {
clearInterval(videoTimer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_yt,
eventAction: 'Finished',
eventLabel: player.getVideoData().title,
nonInteraction: true
});
}
}
}
});
}
});
});
/*
HTML 5 Video Tracker
Work in Progress
*/
$(document).ready(function() {
$(document).arrive('video', function() {
var videoTimer = 0;
var videoPlayLength = 0;
var video_src = $(this).attr('src');
$(this).on('play', function() {
videoTimer = setInterval(function() {
videoPlayLength += adv_analytics_video_timer;
console.log('User watched a video for ' + videoPlayLength + ' seconds for ' + video_src);
if (videoPlayLength > 0) {
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_video5,
eventAction: 'Time Played',
eventLabel: video_src,
eventValue: videoPlayLength,
nonInteraction: true
});
}
}, 1000 * adv_analytics_video_timer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_video5,
eventAction: 'Play',
eventLabel: $(this).attr('src'),
nonInteraction: true
});
});
$(this).on('pause', function() {
clearInterval(videoTimer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_video5,
eventAction: 'Paused',
eventLabel: $(this).attr('src'),
nonInteraction: true
});
});
$(this).on('ended', function() {
clearInterval(videoTimer);
ga('send', {
hitType: 'event',
eventCategory: adv_analytics_category_video5,
eventAction: 'Finished',
eventLabel: $(this).attr('src'),
nonInteraction: true
});
});
});
});
@czenzel
Copy link
Author

czenzel commented Dec 28, 2016

Updated API for IP Address to Another Service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment