Merge data from multiple studies

Programmatically merge data from multiple studies into one Protobi project. 

Select a combined project space

Choose a survey to use as the final combined project space. Data from other studies can be merged into the project, and the existing question order, titles, formats, etc., will still stay the same. If you merge multiple waves of a survey you may want to use the most recent wave as the combined space. When combining different country data, use the survey with the question text and formats that are in your desired language. 

Create a data process

In the data tab of project settings, create a new process. 

Protobi Project settings page for "Example project" with Data section selected in the left sidebar, displaying three columns: Data tables (with "+ New data table" button and "main" table in blue), Processes (with "+ New process" button and "process" labeled as Primary in green), and Documents (with "+ New document" button in gray).

Set the process to asynchronous. This type of data process can retrieve data from multiple projects. 

Protobi "Data table" configuration form titled "process" with a green indicator, showing fields for Name (process), Type (process), Process type dropdown with "Asynchronous" selected (other option "Simple" visible), Updated (Mon Dec 28, 2020 03:13 pm), empty Description field, and Admin buttons: Edit/Run (blue), Delete (gray), and Primary (orange).

Example code

Expand the code panel to copy the data process code seen below. 

This code appends rows of data from multiple surveys vertically, resulting in a larger combined dataset. This is also referred to as "stacked" data. 

Protobi "Data process 'process' (async)" editor with Save (green) and Run... (gray) buttons, displaying JavaScript code that merges data from multiple studies using unique pulseIds, with comments explaining the code retrieves data from two SERMO API endpoints ("60mmp8qs-q4rp-njyu-dii0x63cq40y8ktr8" and "7li6338n-fi9m-1l93-olpd2julmbdv339if") and merges entries based on entry values.

Code

//This data process merges data from multiple studies using the unique pulseIds
var entries = [
    {label: "main", pulseId: "60bmp8gs-q4rp-njyu-dii0x63ca40y8ktr8"},
    {label: "EU",   pulseId: "7l16338n-f19m-1l93-olpd2julmbdv339if"}
]
  
Protobi.get_table("main", function(err, main) {
    if (err)  return callback(err);
    console.log(main)
    async.mapSeries(
        entries,
        function(entry, cb) {
            var url = `https://rtanalytics.sermo.com/api/v3/realtime/${entry.pulseId}/csv`
            console.log(url)

            d3.csv(url, function(rows) {
                if (!rows) return cb(404, "Data not found")
                rows.forEach(function(row) {
                    _.merge(row, entry)  // Set all entry values as row values
                })
                return cb(null, rows)
            })
        },
        function(err, results) {
            if (err)  return callback(err);
            var stacked = _.flatten(results)
            //Reassign questions that differ between surveys (ends before the $.notify)   
            //The loop below operates on each row in the final "stacked" data
            stacked.forEach(function(row) {
                //Assign "temp" name to questions that don't share same key between surveys
                temp = row["Q1"]  
                temp1 = row["Q2"] 
                //Set a condition, reassign questions if data is from EU survey
                if (row.label == "EU") {    
                    //Map the correct "temp" key to the existing question in the project
                    row["Q2"] = temp        
                    row["Q3"] = temp1         
                }
            })  
            //Below returns the stacked data, do not delete    
            $.notify("Success", "success")
            return callback(null, stacked)
        })   
})

In the entries array, add the pulseIds of each survey you want to merge. The example combines two surveys, but you can include more. On line 13 below, the function retrieves data from each survey. The method used here pulls the most recent data. When any of the individual surveys receive more responses, the combined project will also reflect the most up-to-date data. 

Protobi code editor showing lines 1-26 of an asynchronous data process that merges data from multiple studies, defining two survey entries with labels "main" (pulseId: "60mmp8qs-q4rp-njyu-dii0x63cq40y8ktr8") and "EU" (pulseId: "7li6338n-fi9m-1l93-olpd2julmbdv339if"), followed by code to fetch data from SERMO API and merge entries.

Identical questions that have different keys

The surveys may not use the same key for every identical question. On lines 27 - 38 is code that reassigns question keys to align with the main project. 

Notice on lines 30 and 31 temp (temporary) values are assigned to the previous question keys. This is a way to store the value for later use. For instance, in line 35 Q2 is manipulated so the temp1 value is useful in line 36 to reference the original Q2 values. 

Protobi code editor showing lines 26-42 of JavaScript code that processes stacked data, including logic to assign temp names to questions (temp = row["Q1"], temp1 = row["Q2"]) and conditional mapping for EU survey data where questions Q1, Q2, and Q3 are mapped to Q2, Q3 in the combined survey, ending with a success notification and callback.

Reminder: For data processes, "Save" and "Run" the process after you are done editing the code view. To use the result of the process as the primary data for the project, you will need to set it as "Primary".

Data processes are specific to each project, and your code may not look identical to our example.

Support

If you are having trouble merging data, please contact us at support@sermo.freshdesk.com