Skip to content

Instantly share code, notes, and snippets.

@rlespinasse
Last active August 29, 2015 14:06
Show Gist options
  • Save rlespinasse/29d23c412e178a7f6d89 to your computer and use it in GitHub Desktop.
Save rlespinasse/29d23c412e178a7f6d89 to your computer and use it in GitHub Desktop.
API design for Dandelion-GUA
GoogleAnalytics.on(request)
// Get (or Create) Default Tracker (with tracking ID from configuration)
.defaultTracker().goBack()
// Get (or Create) Default Tracker
.defaultTracker("UA-XXXX-Y")
.set(GeneralField.anonymizeIp, "true")
.set(SessionField.sessionControl, "sessionControl").goBack()
// Get (or Create) Another Tracker
.tracker("UA-XXXX-Z", "name-Z")
// Access to "create" section
.create()
.set(new HashMap<AnalyticsCreateField, String>() {{
put(CreateOnlyField.clientId, "clientId");
put(CreateOnlyField.sampleRate, "1");
put(CreateOnlyField.siteSpeedSampleRate, "1");
}})
// Create field set
.set(CreateOnlyField.clientId, "clientId")
.set(CreateOnlyField.sampleRate, "1")
.set(CreateOnlyField.siteSpeedSampleRate, "1")
.set(CreateOnlyField.alwaysSendReferrer, "true").goBack()
// Global field set
.set(GeneralField.anonymizeIp, "true")
.set(SessionField.sessionControl, "sessionControl")
// Access to "send" section with name
.send("nameSend")
// Define a "pageview" send command
.pageview()
.overrideLocation("location")
.overridePage("page")
// Define a "callback" on this command
.callback("callbackFunction", true).goBack().goBack()
// Access to a previous "send" section from her name
.send("nameSend")
// Define a function "timing" send command [function wrapValueFunction(value) ]
.timing("Category", "Var", "wrapValueFunction")
.setLabel("Label")
.set(HitField.nonInteraction, "true").goBack()
.pageview()
.overrideTitle("title")
// In adding, we can define "any" field
.set(ContentInformationField.hostname, "localhost").goBack().goBack()
.send()
// Define a "event" send command
.event("Category", "Action")
.setLabel("Label")
.setValue(4)
.set(HitField.nonInteraction, "true")
// Ask for a link between the "event" send command and a Html Element Id
.onElementId("html_element_id")
// Define a another action for the link
.onAction("AnotherAction")
// Define a "critical callback" on this command
.callback("callbackFunction").goBack().goBack()
// We can define multiple type in one "send" command section
.send()
// Define a "social" send command
.social("network", "action", "target")
.set(ExceptionsField.exDescription, "error").goBack()
// Define a simple "timing" send command
.timing("Category", "Var", 4)
.setLabel("Label")
.set(HitField.nonInteraction, "true").goBack()
// We can add some information in a previous send command (because we are in the same "send" section)
.social("network", "action", "target")
.wrapIntoFunction("functionName").goBack().goBack()
.send()
// Define a "screenview" send command
.screenview()
.setScreenName("ScreenName")
.setAppName("AppName")
.setAppId("AppId")
.setAppVersion("AppVersion")
.setAppInstallerId("AppInstallerId")
.set(HitField.nonInteraction, "true").goBack().goBack()
.send()
// Define a "screenview" send command with a screen name (API says "Optional" but "Recommended"
.screenview("ScreenName").goBack().goBack()
.send()
// Define a "exception" send command
.exception()
.setDescription("Description")
.setFatal(true)
.set(HitField.nonInteraction, "true");
// Access to current stored information
GoogleAnalytics.on(request).tracker("UA-XXXX-A").get(HitField.nonInteraction);
GoogleAnalytics.on(request).tracker("UA-XXXX-A").create().get(HitField.nonInteraction);
GoogleAnalytics.on(request).tracker("UA-XXXX-A").send("previousSendName").pageviewGet(HitField.nonInteraction);
GoogleAnalytics.on(request).tracker("UA-XXXX-A").send("previousSendName").eventGet(EventTrackingField.eventLabel);
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXX-Y', 'auto');
ga('set', 'anonymizeIp', true);
ga('set', 'sessionControl', 'sessionControl');
ga('create', 'UA-XXXX-Z', {
'name': 'name-Z',
'clientId': 'clientId',
'sampleRate': 1,
'siteSpeedSampleRate': 1,
'alwaysSendReferrer': true,
});
ga('name-Z.set', 'anonymizeIp', true);
ga('name-Z.set', 'sessionControl', 'sessionControl');
// send-group : b0c1bba8-024f-4d22-aee4-0165491518c8
ga('name-Z.send', 'exception', {
'exFatal': true,
'exDescription': 'Description',
'nonInteraction': true,
});
// send-group : 7eff3e36-28be-4439-a0df-ccdf8817955d
addListener(document.getElementById('html_element_id', 'AnotherAction', function() {
ga('name-Z.send', 'event', {
'eventAction': 'Action',
'eventCategory': 'Category',
'nonInteraction': true,
'eventValue': 4,
'eventLabel': 'Label',
'hitCallback': callbackFunction,
});
setTimeout(callbackFunctionCritical, 2000);
});
var callbackFunctionCriticalAlreadyCalled = false;
function callbackFunctionCritical() {
if (callbackFunctionCriticalAlreadyCalled) return;
callbackFunctionCriticalAlreadyCalled = true;
callbackFunction();
}
// send-group : 42dccfbb-b87e-4b8a-b7cd-320e07076629
ga('name-Z.send', 'screenview', {
'appId': 'AppId',
'appVersion': 'AppVersion',
'nonInteraction': true,
'screenName': 'ScreenName',
'appInstallerId': 'AppInstallerId',
'appName': 'AppName',
});
// send-group : a80e8c7e-16da-4d02-ba07-f71c5500be90
ga('name-Z.send', 'screenview', {
'screenName': 'ScreenName',
});
// send-group : b498e047-f9cb-4258-b47c-7ec12df56636
function functionName() {
ga('name-Z.send', 'social', {
'exDescription': 'error',
'socialAction': 'action',
'socialTarget': 'target',
'socialNetwork': 'network',
});
});
ga('name-Z.send', 'timing', {
'timingCategory': 'Category',
'nonInteraction': true,
'timingVar': 'Var',
'timingValue': 4,
'timingLabel': 'Label',
});
// send-group : nameSend
ga('name-Z.send', 'pageview', {
'hostname': 'localhost',
'title': 'title',
});
function wrapValueFunction(timingValue) {
ga('name-Z.send', 'timing', {
'timingCategory': 'Category',
'nonInteraction': true,
'timingVar': 'Var',
'timingValue': timingValue,
'timingLabel': 'Label',
});
});
function addListener(element, type, callback) {
if (element.addEventListener) element.addEventListener(type, callback);
else if (element.attachEvent) element.attachEvent('on' + type, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment