Skip to content

Instantly share code, notes, and snippets.

View ttrefren's full-sized avatar

Tim Trefren ttrefren

  • Oakland, CA
View GitHub Profile
// Plain Javascript - only works for a single item per page
window.onload = function() {
mpmetrics.track("Page view", { 'page': document.location });
}
// Or in jQuery (much nicer)
$(document).ready(function() {
mpmetrics.track("Page view", { 'page': document.location });
});
your_function = function(url, data) {
mpmetrics.track('play-game', {'level':'12', 'weapon':'sword', 'character':'knight'});
if (url) {
some = more_random_js;
} else {
even += "more";
}
...
...
<!-- The rest of your HTML code here -->
<input type="button" onclick="mpmetrics.track('play-song', {'genre':'rap', 'artist':'eminem'});">
<script type="text/javascript" src="http://api.mixpanel.com/site_media/js/api/mpmetrics.js"></script>
<script type="text/javascript">
try { mpmetrics.init("YOUR_TOKEN");} catch(err) {}
</script>
...
<a href="javascript:void(0);" onclick="mpmetrics.track('click menu', {'menu item': 'about'},
function() { window.location = 'http://yoursite.com'; });">
<script type="text/javascript" src="http://mixpanel.com/site_media/js/api/mptfb.js"></script>
<script type="text/javascript">
try { mpmetrics.init("YOUR_TOKEN");} catch(err) {}
</script>
...
...
<!-- The rest of your HTML code here -->
<input type="button" onclick="mpmetrics.track('game-start', {'level':'castle', 'char':'knight'})">
your_function = function(url, data) {
mpmetrics.track('battle', {'spell':'fireball', 'damage':'237'});
if (url) {
some = more_random_js;
} else {
even += "more";
}
...
# cURL example
import path.to.logger as mpmetrics
...
def some_function(args):
mpmetrics.track("purchase", {"type": "Easter", "item_number": "3123", "ip": "123.78.98.34"})
...
# Now your regular function code
...
<?php
# cURL example
require_once('path/to/logger.php');
$metrics = new MetricsTracker("YOUR_TOKEN");
...
function some_function($args) {
$metrics->track('menu_click', array('button'=>'learn more',
'color'=>'green',
'ip'=>$_SERVER['REMOTE_ADDR'])
);