Skip to content

Commit ebf5261

Browse files
committed
properly map cohort properties
1 parent 2dd4103 commit ebf5261

File tree

8 files changed

+64
-11
lines changed

8 files changed

+64
-11
lines changed

.clasp.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"scriptId": "1-e_9mTJFnWHvceBDod0OEkYP7B7fgfcxTYqggyoZGLyWOCfWvFge3hZO",
3-
"rootDir": "/Users/ak/code/sheets-mixpanel",
3+
"rootDir": "/Users/jakewymer/sheets",
44
"projectId": "mixpanel-gtm-training"
55
}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ creds.json
88
/tmp/*
99
!tmp/.gitkeep
1010
.env
11-
env.js
11+
env.js
12+
env.gs

Code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
-----------------------------
1212
*/
1313

14-
const APP_VERSION = "1.15";
14+
const APP_VERSION = "1.16";
1515

1616
/**
1717
* some important things to know about google apps script

components/dataExport.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ DATA OUT OF MP
88
* export data; if not called with a config, uses last known
99
*
1010
* @param {MpSheetConfig} [config]
11-
* @returns {[string, ReportMeta | CohortMeta | DashMeta]} string + metadata `[csv, {}]`
11+
* @returns {[string | string[][], ReportMeta | CohortMeta | DashMeta]} string + metadata `[csv, {}]`
1212
*/
1313
function exportData(config) {
1414
//use last known config if unset
@@ -33,7 +33,7 @@ function exportData(config) {
3333
try {
3434
const meta = getCohortMeta(config);
3535
const profiles = getCohort(config);
36-
const csv = JSONtoCSV(profiles);
36+
const csv = profilesToCsvArray(profiles);
3737
return [csv, meta];
3838
} catch (e) {
3939
throw e;
@@ -65,6 +65,7 @@ function exportData(config) {
6565

6666
throw `${type} is unsupported`;
6767
}
68+
6869
/**
6970
* @param {MpSheetConfig} config
7071
* @returns {DashMeta}
@@ -394,5 +395,5 @@ if (typeof module !== "undefined") {
394395
module.exports = { exportData };
395396
const { getConfig } = require("../utilities/storage.js");
396397
const { validateCreds } = require("../utilities/validate.js");
397-
const { JSONtoCSV } = require("../utilities/misc.js");
398+
const { JSONtoCSV, profilesToCsvArray } = require("../utilities/misc.js");
398399
}

env-sample.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,23 @@ const TEST_CONFIG_GROUPS_DATA = [
119119
}
120120
];
121121

122+
/** @type {SheetMpConfigAlways & EventMappings} */
123+
const TEST_CONFIG_AD_SPENT = {
124+
config_type: "sheet-to-mixpanel",
125+
record_type: "event",
126+
event_name_col: "hardcode",
127+
hardcode_event_name: "ad spent",
128+
distinct_id_col: "",
129+
time_col: "timestamp",
130+
insert_id_col: "campaign id",
131+
project_id: PROJECT_ID,
132+
token: TOKEN,
133+
region: "US",
134+
auth_type: "service_account",
135+
service_acct: SERVICE_ACCOUNT,
136+
service_secret: SERVICE_SECRET
137+
};
138+
122139
/** @type {SheetMpConfigAlways & TableMappings} */
123140
const TEST_CONFIG_TABLES = {
124141
config_type: "sheet-to-mixpanel",

tests/all.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ function runTests() {
5252
return Misc.JSONtoCSV([{ foo: "bar", baz: "qux" }]) === expected;
5353
});
5454

55+
test.assert("turns profiles into string[][]", () => {
56+
const expected = [
57+
[`foo`, `baz`, `third`],
58+
[`bar`, `qux`, ``],
59+
[`bar`, ``, `exists`]
60+
];
61+
return JSON.stringify(Misc.profilesToCsvArray([{ foo: "bar", baz: "qux" }, {foo: "bar", third: "exists"}])) === JSON.stringify(expected);
62+
});
63+
5564
test.assert("forms pretty dates?", () => {
5665
const expected = `3/3/1901 @ 4:20am`;
5766
return Misc.formatDate(new Date(1, 2, 3, 4, 20)) === expected;
@@ -218,7 +227,7 @@ function runTests() {
218227
validateCreds(BAD_API_SECRET);
219228
});
220229

221-
test.catchErr("THROWS: bad api project?", `Mismatch between project secret's project ID and URL project ID`, () => {
230+
test.catchErr("THROWS: bad api project?", `Credentials in request did not match project_id URL parameter`, () => {
222231
validateCreds(BAD_PROJECT_API_SECRET);
223232
});
224233

utilities/misc.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@ function JSONtoCSV(arr) {
2020
.join("\n");
2121
}
2222

23+
function profilesToCsvArray(profiles) {
24+
const headers = new Set();
25+
26+
// Need to check all profile keys in case some are missing that prop
27+
profiles.forEach(profile => {
28+
Object.keys(profile).forEach(key => {
29+
headers.add(key);
30+
});
31+
});
32+
33+
const headersArr = Array.from(headers);
34+
35+
const properties = [];
36+
37+
profiles.forEach(profile => {
38+
const profileProps = headersArr.map(header => {
39+
const prop = profile[header] !== undefined ? `${profile[header]}` : "";
40+
return prop;
41+
});
42+
properties.push(profileProps);
43+
})
44+
45+
return [headersArr, ...properties];
46+
}
47+
2348
function sliceIntoChunks(arr, chunkSize = 2000) {
2449
const res = [];
2550
for (let i = 0; i < arr.length; i += chunkSize) {
@@ -105,5 +130,5 @@ function isObject(object) {
105130
}
106131

107132
if (typeof module !== "undefined") {
108-
module.exports = { comma, JSONtoCSV, sliceIntoChunks, formatDate, serial, isDeepEqual, isObject, clone };
133+
module.exports = { comma, JSONtoCSV, sliceIntoChunks, formatDate, serial, isDeepEqual, isObject, clone, profilesToCsvArray };
109134
}

utilities/sheet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@ function getEmptyRow(sheet) {
108108
/**
109109
* overwrites the contents of a spreadsheet with new data
110110
*
111-
* @param {string} csvString
111+
* @param {string | string[][]} csv
112112
* @param {GoogleAppsScript.Spreadsheet.Sheet} sheet
113113
* @returns {SheetInfo}
114114
*/
115-
function overwriteSheet(csvString, sheet) {
116-
var csvData = Utilities.parseCsv(csvString);
115+
function overwriteSheet(csv, sheet) {
116+
const csvData = typeof csv === `string` ? Utilities.parseCsv(csv) : csv;
117117
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
118118
return getSheetInfo(sheet);
119119
}

0 commit comments

Comments
 (0)