Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the script function into the 3.2 version #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ functions that depend on the server Mixpanel, if "available" retrun
their will be a chance that program became stuck: waiting a reponse of
Mixpanel that could never came).

### `Mixpanel.script`
This function execute a pre-defined *Java Script* script in your app, it
is recommended to call this function in server side of your app befor to
use other function if this script is suitable for you. You can see what
this script do in the `base.mli` file.
This script is retruned in the form of an `[> Html_types.script ]
Tyxml_html.elt list` value. It require you to use the `TyXML` librarie,
you can see the [Ocsigen official
documentation](https://ocsigen.org/tyxml/latest/manual/intro) for more
details, more precisely [this
section](https://ocsigen.org/tyxml/4.0/api/Html_sigs.Make.T) for a
better understanding of what this function return.

### `Mixpanel.init`
> This function initializes a new instance of the Mixpanel tracking
object. All new instances are added to the main mixpanel object as sub
Expand Down
4 changes: 3 additions & 1 deletion mixpanel.opam
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
opam-version: "2.0"
version: "3.1.1"
version: "3.2"
maintainer: "Thibaut Gudin <[email protected]>"
authors: "Thibaut Gudin <[email protected]>"
homepage: "https://github.com/besport/ocaml-mixpanel"
Expand All @@ -11,8 +11,10 @@ synopsis: "Binding to the mixpanel plugin using gen_js_api"
build: [[ "dune" "build" "-j" jobs "-p" name "@install" ]]
depends: [
"ocaml" { >= "4.08.0" }
"dune" {>= "2.7"}
"gen_js_api"
"js_of_ocaml"
"js_of_ocaml-ppx"
"lwt_ppx"
"tyxml"
]
46 changes: 46 additions & 0 deletions src/base.mli
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,58 @@ val reset : unit -> unit [@@js.global "mixpanel.reset"]

val available : unit -> bool

val script : (unit -> unit) -> unit

[@@@js.start]

[@@@js.implem
let available () =
Js_of_ocaml.Js.Optdef.test Js_of_ocaml.Js.Unsafe.global##.mixpanel]

[@@@js.implem
open Js_of_ocaml

let script f =
Js.Unsafe.global##.mixpanel
:= object%js
val __e = Js.array [| Js.wrap_callback f |]
end ;
let fjs =
Dom_html.document ## (getElementsByTagName (Js.string "script"))##item 0
in
Js.Opt.iter fjs (fun script ->
Js.Opt.iter script##.parentNode (fun head ->
let js = Dom_html.createScript Dom_html.document in
js##.text :=
Js.string
"(function(c,a){if(!a.__SV){var b=window;try{var \
d,m,j,k=b.location,f=k.hash;d=function(a,b){return(m=a.match(RegExp(b+'=([^&]*)')))?m[1]:null};f&&d(f,'state')&&(j=JSON.parse(decodeURIComponent(d(f,'state'))),'mpeditor'===j.action&&(b.sessionStorage.setItem('_mpcehash',f),history.replaceState(j.desiredHash||'',c.title,k.pathname+k.search)))}catch(n){}var \
l,h;window.mixpanel=a;a._i=[];a.init=function(b,d,g){function \
c(b,i){var \
a=i.split('.');2==a.length&&(b=b[a[0]],i=a[1]);b[i]=function(){b.push([i].concat(Array.prototype.slice.call(arguments,\n\
0)))}}var e=a;'undefined'!==typeof \
g?e=a[g]=[]:g='mixpanel';e.people=e.people||[];e.toString=function(b){var \
a='mixpanel';'mixpanel'!==g&&(a+='.'+g);b||(a+=' \
(stub)');return a};e.people.toString=function(){return \
e.toString(1)+'.people (stub)'};l='disable time_event track \
track_pageview track_links track_forms track_with_groups \
add_group set_group remove_group register register_once alias \
unregister identify name_tag set_config reset opt_in_tracking \
opt_out_tracking has_opted_in_tracking has_opted_out_tracking \
clear_opt_in_out_tracking people.set people.set_once \
people.unset people.increment people.append people.union \
people.track_charge people.clear_charges people.delete_user \
people.remove'.split(' ');\n\
for(h=0;h<l.length;h++)c(e,l[h]);var f='set set_once union \
unset remove delete'.split(' ');e.get_group=function(){function \
a(c){b[c]=function(){call2_args=arguments;call2=[c].concat(Array.prototype.slice.call(call2_args,0));e.push([d,call2])}}for(var \
b={},d=['get_group'].concat(Array.prototype.slice.call(arguments,0)),c=0;c<f.length;c++)a(f[c]);return \
b};a._i.push([b,d,g])};a.__SV=1.2;b=c.createElement('script');b.type='text/javascript';b.async=!0;b.src='undefined'!==typeof \
MIXPANEL_CUSTOM_LIB_URL?\n\
MIXPANEL_CUSTOM_LIB_URL:'file:'===c.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';d=c.getElementsByTagName('script')[0];d.parentNode.insertBefore(b,d)}})(document,window.mixpanel||[]);" ;
js##.async := Js._true ;
Dom.insertBefore head js fjs))]

val set_group :
group_key:string ->
group_ids:string list ->
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
(public_name mixpanel)
(name mixpanel)
(modes byte)
(libraries gen_js_api js_of_ocaml js_of_ocaml-ppx lwt_ppx)
(libraries gen_js_api js_of_ocaml js_of_ocaml-ppx lwt_ppx tyxml)
(preprocess (pps js_of_ocaml-ppx lwt_ppx))
)