Skip to content

Instantly share code, notes, and snippets.

@nitishn
nitishn / tracking.js
Last active August 29, 2015 14:01
Firing an analytics event on a link or form which changes the page sometimes causes the analytics event to be canceled. The proper way to handle this is to use a callback function like below.
var that = this;
event.preventDefault();
var callback = function() {
$(that).parents('#nl_form').submit();
}
_gaq.push(['_set','hitCallback', callback]);
_gaq.push(['_trackEvent', 'Newsletter Subscription', 'Started', window.location.pathname]);
@nitishn
nitishn / app.js
Created September 19, 2014 21:12
JS scroll to a position with Facebook SDK
scrollTo: function ( y ) {
FB.Canvas.getPageInfo(function(pageInfo){
$({y: pageInfo.scrollTop}).animate(
{y: y},
{duration: 500, step: function(offset){
FB.Canvas.scrollTo(0, offset);
}
});
});
},
.button
@extend %button-reset
padding: rem(10px) rem(15px)
color: #ffffff
border-radius: 8px
@extend .clickable
&.is-blue
background: linear-gradient(to bottom, $blue 0%, $blue--dark 100%)
&:hover
public function write_log ( $log ) {
if ( true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
jQuery(document).on('click', '.play', function() {
var video = jQuery(this).data('video-name')
dataLayer.push({'video': video, 'event': 'play_video'});
});
@nitishn
nitishn / compute-xy-click
Created January 26, 2015 01:01
Calculate the position of a click inside an element
$(document).on('click', '.my-class', function(event) {
var position = compute_xy_click(event, this);
});
function compute_xy_click(event, element) {
var position = [];
var gardientPos = $(element).offset();
var posx = 0;
var posy = 0;
if ( !event ) var event = window.event;
@nitishn
nitishn / DataLayer eventCallback
Created January 29, 2015 19:02
Sometimes you need to send a dataLayer push on an external/internal URL. This is how you do it properly.
$(document).on('click', '.widget-promotion a', function(event) {
var title = $(this).text().trim();
var url = $(this).attr('href');
dataLayer.push({
'event': 'promotion_click',
'promotion_title': title,
'eventCallback': function() { window.location = url },
});
});
@nitishn
nitishn / xml-to-csv.php
Created February 9, 2015 21:47
Read an XML file and write out a CSV file in php.
<?php
// How to open an XML file and write out it's contents as a CSV!
// Using sample XML file here https://msdn.microsoft.com/en-us/library/ms762271(d=printer,v=vs.85).aspx
$filexml='books.xml';
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('books.csv', 'w');
foreach($xml->book as $book) {
$values = array(
'author' => $book->author,
@nitishn
nitishn / dates.php
Created May 19, 2016 17:16
Working with dates in WordPress
// Working with dates is annoying. Time is annoying, especially with timezones involved.
// In WP, you have to worry about the timezone set by WordPress AND the timezone set in php.ini.
// A safe bet when working with date logic in WP, is to assume that the timezone set by the WP backend is correct.
// More than likely, an event is local and thus should use WP timezone.
// HOWEVER, when working with php's DateTime class, you need to manually set the timezone or you'll have
// inconsistant dates! Below is a snippet which helps set the timezone, does calculations, and then sets it back.
// Set to WP timezone
$phpTimezone = date_default_timezone_get();
/**
* Grab the upcoming events from the Events Manager plugin. However, we need to make sure only the first
* instance of a recurring event shows up. So, we need to essentially get reccuring/non-recurring events
* and then merge the result set. This is not supported by the plugin currently...
* @return array
*/
function getUpcomingEvents()
{
$upcomingEvents = array();
$recurring = array();