Skip to content

Instantly share code, notes, and snippets.

View jbwyme's full-sized avatar

Josh Wymer jbwyme

  • Mixpanel
View GitHub Profile
@jbwyme
jbwyme / RequireJSOptimizer.java
Last active December 12, 2015 03:59
A Java class to execute the requirejs r.js optimizer with Rhino embedded (vs having to do this from the command line)
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.tools.shell.Global;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
class RequireJSOptimizer {
@jbwyme
jbwyme / Mixpanel PHP API Example Index.php
Last active August 4, 2017 12:32
Mixpanel PHP API Usage
<?php
require_once("/path/to/vendor/mixpanel/mixpanel-php/lib/Mixpanel.php"); // import the Mixpanel class
// get the Mixpanel singelton (will be created if it doesn't exist)
$mp = Mixpanel::getInstance("MIXPANEL_PROJECT_TOKEN");
// this would likely come from a database or session variable
$user_id = 12345;
@jbwyme
jbwyme / mixpanel-2.2-amd-syncronous.html
Last active December 23, 2015 18:39
Mixpanel 2.2 AMD/Require example #1: Sync This method works by creating a pre-init module to setup the window.mixpanel deps needed by the mixpanel lib and then specifying that as a dependency to the lib itself. Then requiring "mixpanel" will block until the lib is fully loaded.
<html>
<head>
<title>Mixpanel AMD Example - Sync</title>
<script type="text/javascript" src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>
<script type="text/javascript">
requirejs.config({
paths : { 'mixpanel': "//cdn.mxpnl.com/libs/mixpanel-2.2.min" },
shim: {
'mixpanel': {
deps: ['mixpanel-preinit'],
@jbwyme
jbwyme / mixpanel-2.2-amd-asyncronous.html
Created September 23, 2013 22:01
Mixpanel 2.2 AMD/Require example #2: Async If you REALLY want async support, you'll need to update your stub's methods once the mixpanel lib is loaded. I don't recommend this because (among other reasons) it results in window.mixpanel !== mixpanel after the copy. This also means you must protect against race conditions on synchronous calls like …
<html>
<head>
<title>Mixpanel AMD Example - Async</title>
<script type="text/javascript" src="http://requirejs.org/docs/release/2.1.8/minified/require.js"></script>
<script type="text/javascript">
requirejs.config({
paths : { 'mixpanel-lib': "//cdn.mxpnl.com/libs/mixpanel-2.2.min" }
});
define("mixpanel", function(require) {
@jbwyme
jbwyme / optimistic-locking-django-model.py
Created December 19, 2013 00:24
A little bit of poc code for optimistic locking within django
class ConcurrentUpdateException(Exception):
"""
Raised when a model can not be saved due to a concurrent update.
"""
class VersionedModel(models.Model):
_version = models.PositiveIntegerField(db_column='version', default=1)
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
"""
@jbwyme
jbwyme / tls.c
Created March 11, 2014 05:09
pthreads with thread local storage
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
pthread_key_t key;
struct args {
char *msg;
};
@jbwyme
jbwyme / mp_async_queue.js
Last active July 27, 2022 22:20
Guarantee mixpanel is loaded
<script type="text/javascript">
// mixpanel stub and initialization
(function(e,b){if(!b.__SV){var a,f,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user".split(" ");
for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=e.createElement("script");a.type="text/javascript";a.async=!0;a.src=("https:"===e.location.protocol?"https:":"http:")+'//cdn.mxpnl.com/libs/mixpanel-2.2.min.js';f=
@jbwyme
jbwyme / SessionManager.java
Created April 18, 2014 19:37
User session manager for android apps
package com.myapp.app;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import org.json.JSONArray;
@jbwyme
jbwyme / get android activities.java
Created June 13, 2014 23:10
Get all activities for android app
private ActivityInfo[] getActivityList() throws PackageManager.NameNotFoundException {
return getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES).activities;
}
@jbwyme
jbwyme / utm_params.php
Created February 28, 2015 01:32
PHP super properties for utm params
// assuming you have already imported the Mixpanel PHP library
$mp = Mixpanel::getInstance("MIXPANEL_PROJECT_TOKEN");
// register every utm parameter as a super property
foreach($_GET as $param => $param_val) {
if (strstr($param, 'utm_') === 0) {
$mp->register($param, $param_val);
}
}