Skip to content

trackWithRedtamarin

Zwetan Kjukov edited this page Oct 8, 2015 · 10 revisions

Tracking with Redtamarin

For when you want to track from the command line.

Use Cases

  • command-line tools and server-side programs
  • to track Windows command-line executables (not supported yet)
  • to track Mac OS X command-line executables
  • to track Linux command-line executables
  • to track Mac OS X shell scripts
  • to track Linux shell scripts
  • to track from ActionScript 3.0 server side
    from CGI, REST API, AMF calls, etc.

Limitations

  • the domain will default to localhost
  • https:// connections over SSL are not fully supported yet
    you will need to use the CurlHitSender instead of the default BSDSocketHitSender
  • maybe some issues with firewall

Optionals

  • queue hit requests when there is no Internet connection

See also: Tracking Scenarios

Basic Setup

Let's test with a shell script, if you need to be quickly setup use as3shebang.

test_analytics1

#!/usr/bin/as3shebang -- 

import shell.*;

// this is to dynamically load the precompiled library
var uanalytics:* = Domain.currentDomain.load( "uanalytics.abc" );
trace( uanalytics + " loaded" ); //you should see "uanalytics/0.8 loaded"

//now you can import uanalytics related classes
import libraries.uanalytics.tracker.*;

//replace with a valid Tracking ID
var trackingID:String = "UA-12345-67"; //we use Web Property

// in a shell script the typing is lose
// and var tracker:CliTracker would create a bug
var tracker = new CliTracker( trackingID ); //hey no type like JS
trace( "trackingId = " + tracker.trackingId );
trace( "  clientId = " + tracker.clientId );

// let's send a pageview
var sent:Boolean = tracker.pageview( "/hello/world", "Hello World" );
trace( "sent = " + sent );

Open your Real-Time reports on the Web Property and run the script
$ ./test_analytics1

You should see the following output:

uanalytics 0.8.0 loaded
trackingId = UA-12345-67
  clientId = 5e8a9e20-28c6-4de2-8d62-545ec9b94f2c
sent = true

Configuration Setup

Let's see how we can configure the tracker.

test_analytics1c

#!/usr/bin/as3shebang -- 

import shell.*;

// this is to dynamically load the precompiled library
var uanalytics:* = Domain.currentDomain.load( "uanalytics.abc" );
trace( uanalytics + " loaded" ); //you should see "uanalytics/0.8 loaded"

//now you can import uanalytics related classes
import libraries.uanalytics.tracker.*;
import libraries.uanalytics.tracking.*;

//replace with a valid Tracking ID
var trackingID:String = "UA-12345-67"; //we use Web Property

// we create a configuration for the tracking
var config = new Configuration();
    config.forcePOST = true;
    config.enableErrorChecking = true;

// we pass the config to the tracker
var tracker = new CliTracker( trackingID, config ); //hey no type like JS
trace( "trackingId = " + tracker.trackingId );
trace( "  clientId = " + tracker.clientId );
trace( "    config = " + tracker.config );

// let's send a pageview
var sent:Boolean = tracker.pageview( "/hello/world/2", "Hello World 2" );
trace( "sent = " + sent );

run it
$ ./test_analytics1c

output should be

uanalytics 0.8.0 loaded
trackingId = UA-12345-67
  clientId = 5e8a9e20-28c6-4de2-8d62-545ec9b94f2c
    config = [object Configuration]
sent = true

Trace Setup

Configure the tracker to trace data instead of sending data.

test_analytics1t

#!/usr/bin/as3shebang -- 

import shell.*;

// this is to dynamically load the precompiled library
var uanalytics:* = Domain.currentDomain.load( "uanalytics.abc" );
trace( uanalytics + " loaded" ); //you should see "uanalytics/0.8 loaded"

//now you can import uanalytics related classes
import libraries.uanalytics.tracker.*;
import libraries.uanalytics.tracking.*;

//replace with a valid Tracking ID
var trackingID:String = "UA-12345-67"; //we use Web Property

// we create a configuration for the tracking
var config = new Configuration();
	config.forcePOST = true;
	config.enableErrorChecking = true;
	config.senderType = "libraries.uanalytics.tracker.senders.TraceHitSender";

// we pass the config to the tracker
var tracker = new CliTracker( trackingID, config ); //hey no type like JS
trace( "trackingId = " + tracker.trackingId );
trace( "  clientId = " + tracker.clientId );
trace( "    config = " + tracker.config );

// let's send a pageview
var sent:Boolean = tracker.pageview( "/hello/world/3", "Hello World 3" );
trace( "sent = " + sent );

run it
$ ./test_analytics1t

output should be

uanalytics 0.8.0 loaded
trackingId = UA-12345-67
  clientId = 5e8a9e20-28c6-4de2-8d62-545ec9b94f2c
    config = [object Configuration]
request:
--------
User-Agent: user_agent_string
POST http://www.google-analytics.com/collect
v=1&_v=as3uanalytics1&cid=5e8a9e20-28c6-4de2-8d62-545ec9b94f2c&dh=alpha.local&dp=%2Fhello%2Fworld%2F3&ds=commandline&dt=Hello%20World%203&t=pageview&tid=UA-94526-31

  v = 1
  _v = as3uanalytics1
  cid = 5e8a9e20-28c6-4de2-8d62-545ec9b94f2c
  dh = local.machine
  dp = /hello/world/3
  ds = commandline
  dt = Hello World 3
  t = pageview
  tid = UA-12345-67

--------
Error: hit not sent
	at libraries.uanalytics.tracker.senders::TraceHitSender/send()
	at libraries.uanalytics.tracker::CommandLineTracker/send()
	at libraries.uanalytics.tracker::CommandLineTracker/pageview()
	at global$init()[:29]
	at shell::Runtime$/eval()
	at global$init()

Debug Setup

You can also use a debug hit sender that will send the data to the Measurement Protocol Validation Servers.

test_analytics1d

#!/usr/bin/as3shebang -- 

import shell.*;

// this is to dynamically load the precompiled library
var uanalytics:* = Domain.currentDomain.load( "uanalytics.abc" );
trace( uanalytics + " loaded" ); //you should see "uanalytics/0.8 loaded"

//now you can import uanalytics related classes
import libraries.uanalytics.tracker.*;
import libraries.uanalytics.tracking.*;

//replace with a valid Tracking ID
var trackingID:String = "UA-12345-67"; //we use Web Property

// we create a configuration for the tracking
var config = new Configuration();
	config.forcePOST = true;
	config.enableErrorChecking = true;

/* ATTENTION:
   We HAVE TO use another sender that supports HTTPS
   as the validation servers can be accessed only with https://
*/
	config.senderType = "libraries.uanalytics.tracker.senders.CurlHitSender";

// if you want to see results on the standard output
var verbose:Boolean = true;

// if you want to use the validation servers
var debug:Boolean = true;

// we pass the config to the tracker
// and also the additional args verbose and debug
var tracker = new CliTracker( trackingID, config, verbose, debug ); //hey no type like JS
trace( "trackingId = " + tracker.trackingId );
trace( "  clientId = " + tracker.clientId );
trace( "    config = " + tracker.config );

// let's send a pageview
var sent:Boolean = tracker.pageview( "/hello/world/4", "Hello World 4" );
trace( "sent = " + sent );

run it
$ ./test_analytics1d

output should be

uanalytics 0.8.0 loaded
trackingId = UA-12345-67
  clientId = 5e8a9e20-28c6-4de2-8d62-545ec9b94f2c
    config = [object Configuration]
command-line:
--------
curl -s -X POST -d 'v=1&_v=as3uanalytics1&cid=5e8a9e20-28c6-4de2-8d62-545ec9b94f2c&dh=alpha.local&dp=%2Fhello%2Fworld%2F4&ds=commandline&dt=Hello%20World%204&t=pageview&tid=UA-12345-67' 'https://www.google-analytics.com/debug/collect'
--------

response:
--------
{
  "hitParsingResult": [ {
    "valid": true,
    "parserMessage": [ ],
    "hit": "/debug/collect?v=1\u0026_v=as3uanalytics1\u0026cid=5e8a9e20-28c6-4de2-8d62-545ec9b94f2c\u0026dh=alpha.local\u0026dp=/hello/world/4\u0026ds=commandline\u0026dt=Hello World 4\u0026t=pageview\u0026tid=UA-12345-67"
  } ],
  "parserMessage": [ {
    "messageType": "INFO",
    "description": "Found 1 hit in the request."
  } ]
}

--------

sent = true

Advanced Setup

As you can see from the trace and debug, our hit request is valid but we don't track much.

In your reports you will be able to see the Operating System because we fake a good user-agent by default on the command-line, but you will not see informations about the User Language for example.

Let's add more tracking.

test_analytics1a

run it
$ ./test_analytics1a

output should be