Solved

How to merge 2 ids after turning ID Merge feature one?

  • 27 March 2020
  • 5 replies
  • 190 views

Badge

I’m not quite sure how to merge 2 ids together after turning on the Merge ID feature and need help on this.

 

My project already have the ID merge enable.  But when I tried to merge 2 user distinct ids together using both merge() and identify(), somehow it didn’t work.

I have two users for testing: TMID1 and TMID2 (both have different USER_ID, but share the same email).

I tried merge() (using http://api.mixpanel.com/import/?data=XXXX with below JSON object) and got 0 as a result. 

JSON object for merge()

{
  "event": "$merge",
  "properties": {
        "$distinct_ids": ["TMID1", "TMID2"],
            "token": "myproject-token"
  }
}

I also tried identify() (using http://api.mixpanel.com/track/?data=XXXX with below JSON object) and got 1 as a result.

JSON object for identify()

{
  "event": "$identify",
  "properties": {
    "$identified_id": "TMID1",
    "$anon_id": "TMID2",
        "token": "myproject-token"
  }
}

For identify(), even though the result was 1, there was a failure with reason “errAnonDistinctIdAssignedAlready” in the detail of event “identify” under the user TMID1.  And when I list all users the the project, I can still see both users in the list (as they were merged together).  I don’t think I understand clearly how to use identify() in merging context.

 

icon

Best answer by ian 28 March 2020, 18:14

@noom  Thanks for trying that out.  

I just used the below:

{
"event": "$merge",
"properties": {
"$distinct_id" : "",
"time": 1585378800,
"$distinct_ids": ["merge1", "merge2"],
"token": "my_project_token"
}
}

(time is just midnight today)

And ran the curl request as such:

curl https://api.mixpanel.com/import \
-u my_api_secret: \
-d data=ewogICJldmVudCI6ICIkbWVyZ2UiLAogICJwcm9wZXJ0aWVzIjogewogIAkJIiRkaXN0aW5jdF9pZCIgOiAiIiwKICAJCSJ0aW1lIjogMTU4NTM3ODgwMCwKICAgIAkiJGRpc3RpbmN0X2lkcyI6IFsibWVyZ2UxIiwgIm1lcmdlMiJdLAogICAgCSJ0b2tlbiI6ICI0MzAxYjg4Mzc2Zjk1MjYwOTQ1ODU0YmY1M2Q1ZGU3ZiIKICB9Cn0= \
-d verbose=1

Without error:

 

View original

5 replies

Userlevel 1
Badge +1

Hi there,

Using the $merge event is the right approach.  The $identify is failing because the anon id needs to be a UUID format.

Nonetheless, when importing events, we expect a $distinct_id and a time property.  Can you add those two in addition to what you currently have? You can keep the $distinct_id as an empty string and just pull in an epoch timestamp for the current time.  

Can you give the above a try and let me know if that works?

Best,

Ian

Hello!

In addition to what Ian mentioned above, I think the reason $merge did not work for you because the server is rejecting your request, since you mentioned you were getting a “0” as the response. This is usually because a required field is missing or the payload is not in the correct format.

Tracking Events

Each event you record is represented as a JSON object in a request to https://api.mixpanel.com/track/. The request will return an HTTP response with body "1" if the track call is successful, and a "0" otherwise. 

https://developer.mixpanel.com/docs/http#section-tracking-events

 

One tip that you may find useful is passing the “verbose=1” parameter when you’re sending the $merge event to /import API. It will return a more descriptive reason for why it rejected your $merge event. 

Please take a look at https://developer.mixpanel.com/docs/http#section-event-request-parameters to see the details on the verbose parameter.

Badge

@ian ,

identify() returns 1 as success but with error in #3 from the picture.

merge() returns 0 which is no success.

 

@ian , @entombedvirus 

I tried adding verbose=1 as suggested by @entombedvirus and got the return as {"error":"api_key, missing or empty","status":0}.

Then I tried to add “api_key” with below JSON object and it still didn’t work.

{
"event": "$merge",
"properties": {
"$distinct_ids": ["TMID1", "TMID2"],
"token": "myproject-token",
"api_key": "myproject-apikey"
}
}

Project token” and “api-key” are from here:

 

And about “time” in Mixpanel document (https://developer.mixpanel.com/docs/http#section-reserved-properties-in-events), it is optional.

I also tried adding $distinct_id and replace “TMID1” and “TMID2” with UUIDs and still got the same error as {"error":"api_key, missing or empty","status":0}.

{
"event": "$merge",
"properties": {
"$distinct_id": "1711ce67f4f141-xxxx-xxxx-xxxx-1711ce67f502a6",
"$distinct_ids": ["1711ce67f4f141-xxxx-xxxx-xxxx-1711ce67f502a6", "1711cefa5524b9-xxxx-xxxx-xxxx-1711cefa5535ab"],
"token": "my-project-token",
"api_key": "my-project-api-key"
}
}

In my opinion, example in the Mixpanel website below doesn’t really work and should provide more detail of what other properties are required in what format to make the call works. 

{
"event": "$merge",
"properties": {
"$distinct_ids": ["your_id", "other_distinct_id"],
"token": "e3bc4100330c35722740fb8c6f5abddc"
}
}

 

Any suggestion?

Userlevel 1
Badge +1

@noom  Thanks for trying that out.  

I just used the below:

{
"event": "$merge",
"properties": {
"$distinct_id" : "",
"time": 1585378800,
"$distinct_ids": ["merge1", "merge2"],
"token": "my_project_token"
}
}

(time is just midnight today)

And ran the curl request as such:

curl https://api.mixpanel.com/import \
-u my_api_secret: \
-d data=ewogICJldmVudCI6ICIkbWVyZ2UiLAogICJwcm9wZXJ0aWVzIjogewogIAkJIiRkaXN0aW5jdF9pZCIgOiAiIiwKICAJCSJ0aW1lIjogMTU4NTM3ODgwMCwKICAgIAkiJGRpc3RpbmN0X2lkcyI6IFsibWVyZ2UxIiwgIm1lcmdlMiJdLAogICAgCSJ0b2tlbiI6ICI0MzAxYjg4Mzc2Zjk1MjYwOTQ1ODU0YmY1M2Q1ZGU3ZiIKICB9Cn0= \
-d verbose=1

Without error:

 

Badge

@ian ,

Thank you, it’s all work now.  :smiley:

I ran my line using curl request and got the result as “{"error":null,"status":1}”.  That is fantastic. 

But I wonder why I cannot not run “https://api.mixpanel.com/import/?data=XXX” on the browser or Postman.

Reply