Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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;
jQuery(document).on('click', '.play', function() {
var video = jQuery(this).data('video-name')
dataLayer.push({'video': video, 'event': 'play_video'});
});
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 );
}
}
}
.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
@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);
}
});
});
},
@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]);
// build the google plus feed template
$.ajax({
url : that.default_settings['api_path'] + 'gplus/',
}).done(function( results ) {
// compile the facebook feed template
feed_template = $(".feed_template").html();
@nitishn
nitishn / app.js
Created March 19, 2014 00:20
A "deffered" bitly URL shortener.
var utility = {
app_data: {
bitlyAccessToken: 'the-key',
bitlyBaseUrl: 'https://api-ssl.bitly.com',
vanityUrl: '',