Assuming you have completed the Lua Tracker Setup for your Lua project, you are now ready to initialize the Lua Tracker.
Requiring the module
Require the Lua Tracker”s module into your Lua code like so:
local snowplow = require( "snowplow" )
Code language: JavaScript (javascript)
That’s it – you are now ready to initialize a tracker instance.
Creating a tracker
There are two different versions of the tracker constructor, depending on which type of collector you want to log events to.
If you are using a Cloudfront collector, use newTrackerForCf() to initialize your tracker instance. If you are using any other collector (e.g. the Clojure Collector or the Scala Stream Collector), then you should use newTrackerForUri().
Create a tracker logging to Cloudfront with newTrackerForCf()
You can initialize a tracker instance for a Cloudfront collector with:
local t = snowplow.newTrackerForCf( "{{CLOUDFRONT-SUBDOMAIN}}" )
Code language: JavaScript (javascript)
So if your Cloudfront subdomain is d3rkrsqld9gmqf
, you would write:
local t = snowplow.newTrackerForCf( "d3rkrsqld9gmqf" )
Code language: JavaScript (javascript)
This completes the initialization of your tracker instance.
Create a tracker logging to a non-CloudFront collector using newTrackerForUri()
You can initialize a tracker instance for a non-Cloudfront collector with:
local t = snowplow.newTrackerForUri( "{{COLLECTOR-URI}}" )
Code language: JavaScript (javascript)
So if your collector is available at “my-company.c.snplow.com”, you would write:
local t = snowplow.newTrackerForUri( "my-company.c.snplow.com" )
Code language: JavaScript (javascript)
This completes the initialization of your tracker instance.
Creating multiple trackers
Each tracker instance is completely sandboxed, so you can create multiple trackers as you see fit.
Here is an example of instantiating two separate trackers:
local t1 = snowplow.newTrackerForCf( "d3rkrsqld9gmqf" )
t1:platform( "cnsl" )
t1:trackUnstructEvent( "save-game", { save_id = 23 }, 1369330092 )
local t2 = snowplow.newTrackerForUri( "cc-endpoint.mysite.com" )
t2:platform( "cnsl" )
t2:trackScreenView( "Game HUD", "23" )
t1:trackScreenView( "Test", "23" ) -- Back to first tracker
Code language: JavaScript (javascript)