Integrate with Mixpanel

Learn how to integrate Flagship with Mixpanel

How to push data to Mixpanel?

You might want to send data from Flagship to Mixpanel, to push experiments and features information and combine them with all your analytics dashboard. Hopefully, the method we're providing will help you reach that goal.

📘

Reminder

The following custom integration isn’t under maintenance. It has been design to be generic, to work on the largest set of cases and codebase.
As a Yoda Master developer, you are the king of your own code.

The following code example takes into account that you already started your Flagship SDK and synchronized your user to get the flag (the modification) he should be assigned to.
The user is now seeing the flag, and you need to activate it, to alert us and count one more user in the corresponding report.
When activating it with our Flagship method, you will trigger the Mixpanel method at the same time and send the information you need to, like in the code example below.

It also implies that you imported the Mixpanel library before calling their method.

// Flagship campaign activation

        Flag<String> myFlag = flagshipVisitor.getFlag("myFlag", "defaultValue");
        myFlag.userExposed();

// Get Flagship campaign information, map it, and send it to Mixpanel

        MessageBuilder messageBuilder = new MessageBuilder("MIXPANEL_TOKEN");
        MixpanelAPI mixpanel = new MixpanelAPI();
        ClientDelivery clientDelivery = new ClientDelivery();

        JSONObject campaignMetadata = myFlag.metadata().toJSON();
        if (properties != null) {
            clientDelivery.addMessage(messageBuilder.event("distinctId", "flagship_event", campaignMetadata));
            mixpanel.deliver(clientDelivery);
        }
//... some other codes

Flagship.start(ENV_ID, API_KEY, {
  onUserExposure: ({ flagData, visitorData }) => {
    mixpanel.track('flagship_event', {
      distinct_id: visitorData.visitorId,
      ...flagData.metadata
    })
  }
})

//... do some stuff

// Then Calling flag.getValue() or flag.userExposed() anywhere will trigger onUserExposure callback
// Flagship campaign activation

        val myFlag = visitor.getFlag("myFlag", "defaultValue")
        myFlag.userExposed()

// Get Flagship campaign information, map it, and send it to Mixpanel

        mixpanel.track("flagship_event", myFlag.metadata().toJson())
// We assume you created visitor and fetched flags

// Retrieve the current visitor
FSVisitor * currentVisitor = [[Flagship sharedInstance] sharedVisitor];

// Get Flag object
FSFlag * myFlagObject = [currentVisitor getFlagWithKey:@"my_flag" defaultValue:@"defaultValue"];

// Expose the campaign
[myFlagObject userExposed];

// Get flagship campaign information and send it to Mixpanel
NSDictionary * campaign_metadata =  [[myFlagObject metadata] toJson];
[mixpanel track:@"flagship_event" campaign_metadata];
<?php

// Flagship get flag

$flag = $fsVisitorInstance->getFlag('my_flag', 'defaultValue');

// Flagship campaign activation

$flag->getValue();
//or 
$flag->userExposed();

// Get Flagship campaign information and send it to Segment

$mp = Mixpanel::getInstance("MIXPANEL_TOKEN");

$campaignInfo = $flag->getMetadata();

$mp->track("flagship_event", $campaignInfo);
// We assume you created visitor and fetched flags

// Retrieve the current visitor

if let currentVisitor = Flagship.sharedInstance.sharedVisitor{
    
    // Get the flag object
    let myFlagObject = currentVisitor.getFlag(key: "my_flag", defaultValue:"defaultValue")
    
    // Expose the campaign
    myFlagObject.userExposed()
    
    // Get flagship campaign information and send it to Mixpanel
    if let campaign_metadata =  myFlagObject.metadata().toJson() as? [String:MixpanelType]{
        
        Mixpanel.mainInstance().track(event: "flagship_event", properties:campaign_metadata)
    }
}
<FlagshipProvider 
   onUserExposure={({ flagData, visitorData }) => {
    mixpanel.track('flagship_event', {
      distinct_id: visitorData.visitorId,
      ...flagData.metadata
    })
  }}
>
 //... do some stuff

// Then Calling flag.getValue() or flag.userExposed() anywhere will trigger onUserExposure callback
</FlagshipProvider>

Any feedback? We would be really happy to have a quick chat!


What’s Next