Created
April 21, 2020 21:14
-
-
Save cswalina/b2651fa04095232355936b16092bb919 to your computer and use it in GitHub Desktop.
mixpanel integration examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We usually store tokens in the app in config/mixpanle.yml: | |
development: | |
token: 1234xyz | |
test: | |
token: abc123 | |
And put the top-level Mixpanel code snippet in a shared partial, "app/views/shared/_mixpanel.html.erb": | |
<% if mixpanel_enabled? %> | |
<!-- start Mixpanel --><script type="text/javascript">(function(e,a){if(!a.__SV){var b=window;try{var c,l,i,j=b.location,g=j.hash;c=function(a,b){return(l=a.match(RegExp(b+"=([^&]*)")))?l[1]:null};g&&c(g,"state")&&(i=JSON.parse(decodeURIComponent(c(g,"state"))),"mpeditor"===i.action&&(b.sessionStorage.setItem("_mpcehash",g),history.replaceState(i.desiredHash||"",e.title,j.pathname+j.search)))}catch(m){}var k,h;window.mixpanel=a;a._i=[];a.init=function(b,c,f){function e(b,a){var c=a.split(".");2==c.length&&(b=b[c[0]],a=c[1]);b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments, | |
0)))}}var d=a;"undefined"!==typeof f?d=a[f]=[]:f="mixpanel";d.people=d.people||[];d.toString=function(b){var a="mixpanel";"mixpanel"!==f&&(a+="."+f);b||(a+=" (stub)");return a};d.people.toString=function(){return d.toString(1)+".people (stub)"};k="disable time_event track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config reset people.set people.set_once people.increment people.append people.union people.track_charge people.clear_charges people.delete_user".split(" "); | |
for(h=0;h<k.length;h++)e(d,k[h]);a._i.push([b,c,f])};a.__SV=1.2;b=e.createElement("script");b.type="text/javascript";b.async=!0;b.src="undefined"!==typeof MIXPANEL_CUSTOM_LIB_URL?MIXPANEL_CUSTOM_LIB_URL:"file:"===e.location.protocol&&"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//)?"https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js":"//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js";c=e.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}})(document,window.mixpanel||[]); | |
mixpanel.init("<%= mixpanel_token %>");</script><!-- end Mixpanel --> | |
<% end %> | |
We include the partial in: | |
"app/views/layouts/application.html.erb" | |
We can track users' behavior by envoking different Mixpanel calls/requests. In the past we've organized these tracking functions as a set of helpers which we can then inject into specific views. Here's an example helper file: | |
"app/helpers/mixpanel_helper.rb" | |
------------------------------------------------------------------------------------------------------------------------- | |
# A set of helpers for injecting Mixpanel tracking codes into pages. | |
module MixpanelHelper | |
# Use this to track an event. Name the event in plain English so that it | |
# looks good in the reports. Properties are an arbitrary hash of helpful | |
# data to attach to the event -- ideally things that can be used for | |
# filtering later. | |
def mixpanel_track(event, properties={}) | |
return nil unless mixpanel_enabled? | |
properties_json = ActiveSupport::JSON.encode(properties) | |
%{ | |
<script type="text/javascript"> | |
mixpanel.track("#{event}", #{properties_json}); | |
</script> | |
}.html_safe | |
end | |
# Call this when we discover the email address of the current user, so | |
# that we can map their anonymous behavior to an identity. | |
# Ref: https://goo.gl/ZGw4oJ | |
def mixpanel_alias(email) | |
return nil unless mixpanel_enabled? | |
%{ | |
<script type="text/javascript"> | |
mixpanel.alias("#{email}"); | |
</script> | |
}.html_safe | |
end | |
# Assign properties to the current user, such as their email address, | |
# name, etc. Mixpanel has a few "special" properties that start with a | |
# dollar sign, such as $email, $first_name, and $last_name that are used | |
# in various reports and other functions. | |
def mixpanel_people_set(properties={}) | |
return nil unless mixpanel_enabled? | |
properties_json = ActiveSupport::JSON.encode(properties) | |
%{ | |
<script type="text/javascript"> | |
mixpanel.people.set(#{properties_json}); | |
</script> | |
}.html_safe | |
end | |
# Designed to accept a Money object or dollar number (not a string). | |
# Quantifies the amount of money raised from this user. | |
def mixpanel_track_charge(amount) | |
return nil unless mixpanel_enabled? | |
%{ | |
<script type="text/javascript"> | |
mixpanel.people.track_charge(#{amount}); | |
</script> | |
}.html_safe | |
end | |
def mixpanel_enabled? | |
mixpanel_token.present? | |
end | |
def mixpanel_token | |
Rails.application.config_for(:mixpanel)['token'] | |
end | |
end | |
------------------------------------------------------------------------------------------------------------------------- | |
In the view we inject the helper functions like this: | |
<%= mixpanel_track("Visit Home") %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment