23 August 2013

jQuery Mobile and Google Analytics

Working from the tech.agilitynerd blog I needed to make some changes.

I am following the GA instructions and loading the aynschronous code via a PHP file, but I find that the minified code given by GA directly does not work, so am using the code snippets from the documentation instead.

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7920205-5']);
//_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);
})();

Note that I commented out the _trackPageview call, as otherwise I register the first page twice.
Then I have the following 

$(document).on('pageshow', "div:jqmData(role='page')", function (event, ui) {
var url = '';
var hash;
    try {
console.log('ga '+url);
    _gaq.push(['_trackPageview', url]);
} catch(err) {}
});

I use the pageshow event rather than pageinit as the URL is only updated by the latter event.

18 August 2013

Find, change & replace in garmin .fit file

I recently did a long ride and discovered at the end that the speed sensor had been badly configured (wheel circumference set to 6 times too large). This was leading to distorted figures in the Training Centre.  Needing to edit several lines in the .fit file i turned to perl nad this script

#!/usr/bin/env perl
use strict;
use warnings;
my $file='knokke.xml';
open my $info, $file or die "Could not open $file: $!";
open my $out, '>', "knokke_new.xml" or die "Can't write new file: $!";
while( my $line = <$info>)  {  
my $num = $1 if $line =~ /\(.*)\<\/DistanceMeters\>/;
if ( $num ) {
    my $newnum = $num / 750.5*140;
$line =~ s/$num/$newnum/;
}
print $out $line;
}
close $file;
close $out;

6 August 2013

Convert OpenStreetMap data for Mac

Download a map.osm data file using the OpenStreetMap export option - it only seems to permit small areas.

Main instructions are here, but I also had to follow this workaround having got an error "Failed to read option file". That resulted in a gmapsub (and several other files), but not something that could be used with Basecamp.

I then used gmapibuilder, to automagically combine all the outputs into a gmapi file.  Using the cyclemap style files the end result looked like


4 May 2013

OSM cycling tagging - some notes



One way streets that bikes can use in both directions - oneway:bicycle=no

Tagging routes to prefer/avoid - class:bicycle=1 class:bicycle:roadcycle=-2

Cycleway and cycletracks

Road with a cycleway on just 1 side




27 January 2013

Regular expressions

MySQL

Change telephone numbers of format 123 4567 to 020 123 4567

SELECT qname, tel FROM `restaurants` WHERE tel REGEXP  '^[0-9]{3}\ [0-9]{4}$'

UPDATE `restaurants` SET `tel` = CONCAT("020", telWHERE tel REGEXP  '^[0-9]{3}\ [0-9]{4}$'

Javascript

//$content = preg_replace('/(\[caption.*\])/', '', $content);
// () Grouping
// [ ] Character class
// ^ in [ ] is a NOT
// . Match any character (except newline)
// * Match preceding character 0 or more times
// + Match preceding character 1 or more times
// ? Match 1 or 0 times
// | OR(?)
// $ match end of input
// /i 
(.+?) = match any charchter more than once
// \s Match any white space character
// preg_replace('/(.+?)+(<\/tag>)/i', '', $string);
//$imgs = preg_match('/]+\>/i', $content);
//$howMany = preg_match_all('/]+>/i', $content, $matches, PREG_PATTERN_ORDER);

//$howMany = preg_match_all('/alt=\"([^\"]+)\".*src=\"([^\"]+)\".*width=\"([^\"]+)\".*height=\"([^\"]+)\"/i', $content, $images, PREG_PATTERN_ORDER);