mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 14:05:24 +02:00
fix(parser): implement regex matching for headers to catch prefixed metric titles like '7 day total sales'
This commit is contained in:
+31
-21
@@ -236,7 +236,7 @@ const normalizeMonth = (rawMonth: string): string => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Robust CSV Column Value Extractor
|
// Robust CSV Column Value Extractor
|
||||||
const getColumnValue = (row: any, aliases: string[]): string => {
|
const getColumnValue = (row: any, aliases: (string | RegExp)[]): string => {
|
||||||
const rowKeys = Object.keys(row);
|
const rowKeys = Object.keys(row);
|
||||||
const normalizedRowKeys: Record<string, string> = {};
|
const normalizedRowKeys: Record<string, string> = {};
|
||||||
|
|
||||||
@@ -248,14 +248,24 @@ const getColumnValue = (row: any, aliases: string[]): string => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
for (const alias of aliases) {
|
for (const alias of aliases) {
|
||||||
|
if (alias instanceof RegExp) {
|
||||||
|
// Find the first original key that matches the regex
|
||||||
|
const matchedKey = rowKeys.find(k => alias.test(k));
|
||||||
|
if (matchedKey) {
|
||||||
|
const val = row[matchedKey];
|
||||||
|
if (val !== undefined && val !== null) {
|
||||||
|
const strVal = String(val).trim();
|
||||||
|
if (strVal.length > 0) return strVal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
const lookup = alias.toLowerCase().replace(/[^a-z0-9]/g, '');
|
const lookup = alias.toLowerCase().replace(/[^a-z0-9]/g, '');
|
||||||
if (normalizedRowKeys[lookup]) {
|
if (normalizedRowKeys[lookup]) {
|
||||||
const actualKey = normalizedRowKeys[lookup];
|
const actualKey = normalizedRowKeys[lookup];
|
||||||
const val = row[actualKey];
|
const val = row[actualKey];
|
||||||
if (val !== undefined && val !== null) {
|
if (val !== undefined && val !== null) {
|
||||||
const strVal = String(val).trim();
|
const strVal = String(val).trim();
|
||||||
if (strVal.length > 0) {
|
if (strVal.length > 0) return strVal;
|
||||||
return strVal;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -436,15 +446,15 @@ export const processAdsCSV = (file: File): Promise<AdsRecord[]> => {
|
|||||||
const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']);
|
const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']);
|
||||||
const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']);
|
const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']);
|
||||||
const asin = getColumnValue(row, ['asin']);
|
const asin = getColumnValue(row, ['asin']);
|
||||||
const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', 'ad spend']);
|
const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', /ad\s*spend/i, /cost/i]);
|
||||||
const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics']);
|
const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics', /click/i]);
|
||||||
const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp']);
|
const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp', /impression/i]);
|
||||||
const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic']);
|
const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic', /cost.*per.*click/i, /cpc/i]);
|
||||||
const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate']);
|
const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate', /click.*through.*rate/i, /ctr/i]);
|
||||||
const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS']);
|
const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS', /cost.*of.*sales/i, /acos/i]);
|
||||||
const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders']);
|
const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders', /order/i, /conversion/i]);
|
||||||
const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units']);
|
const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units', /unit/i, /einheit/i, /unidad/i]);
|
||||||
const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)']);
|
const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)', /sale/i, /umsatz/i, /ventas/i]);
|
||||||
|
|
||||||
if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue;
|
if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue;
|
||||||
|
|
||||||
@@ -510,15 +520,15 @@ export const processAdsExcel = async (fileOrBuffer: File | ArrayBuffer): Promise
|
|||||||
const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']);
|
const countryRaw = getColumnValue(row, ['country', 'marketplace', 'portfolio', 'customer', 'kunde']);
|
||||||
const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']);
|
const weekRaw = getColumnValue(row, ['week', 'woche', 'semana']);
|
||||||
const asin = getColumnValue(row, ['asin']);
|
const asin = getColumnValue(row, ['asin']);
|
||||||
const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', 'ad spend']);
|
const costRaw = getColumnValue(row, ['cost', 'spend', 'ausgaben', 'gasto', 'coste', /ad\s*spend/i, /cost/i]);
|
||||||
const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics']);
|
const clicksRaw = getColumnValue(row, ['clicks', 'klicks', 'clics', /click/i]);
|
||||||
const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp']);
|
const impressionsRaw = getColumnValue(row, ['impressions', 'impresiones', 'imp', /impression/i]);
|
||||||
const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic']);
|
const cpcRaw = getColumnValue(row, ['cpc', 'cost-per-click', 'coste por clic', /cost.*per.*click/i, /cpc/i]);
|
||||||
const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate']);
|
const ctrRaw = getColumnValue(row, ['ctr', 'click-through rate', 'click-through-rate', 'click through rate', /click.*through.*rate/i, /ctr/i]);
|
||||||
const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS']);
|
const acosRaw = getColumnValue(row, ['acos', 'advertising cost of sales', 'aCOS', /cost.*of.*sales/i, /acos/i]);
|
||||||
const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders']);
|
const conversionsRaw = getColumnValue(row, ['conversions', 'konversionen', 'orders', 'pedidos', 'total orders', /order/i, /conversion/i]);
|
||||||
const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units']);
|
const unitsRaw = getColumnValue(row, ['units', 'einheiten', 'unidades', 'units sold', 'total units', /unit/i, /einheit/i, /unidad/i]);
|
||||||
const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)']);
|
const salesRaw = getColumnValue(row, ['sales', 'umsatz', 'ventas', 'ad sales', 'total sales', 'sales (30d)', /sale/i, /umsatz/i, /ventas/i]);
|
||||||
|
|
||||||
// Skip if missing essential data
|
// Skip if missing essential data
|
||||||
if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue;
|
if (!asin || !countryRaw || weekRaw === undefined || weekRaw === '') continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user