Skip to content

Commit

Permalink
add initial support for matomo reaggregation metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Jun 23, 2024
1 parent 7c76580 commit 7eb853b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface ReportMetadata {
metricTypes?: Record<string, string>;
metricTypesGoal?: Record<string, string>;
parameters?: Record<string, string>;
metricAggregationTypes?: Record<string, string>;
metricAggregationTypesGoal?: Record<string, string>;
}

export interface Goal {
Expand Down
24 changes: 23 additions & 1 deletion src/schema/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

import AggregationType = GoogleAppsScript.Data_Studio.AggregationType;
import cc from '../connector';
import dayjs from "dayjs/esm";
import dayjs from 'dayjs/esm';
import { log } from '../log';

export const MATOMO_SEMANTIC_TYPE_TO_LOOKER_MAPPING = {
'dimension': cc.FieldType.TEXT,
Expand Down Expand Up @@ -97,3 +99,23 @@ export function convertMatomoTypeToLooker(value: any, matomoType: string) {
// fail to display the data.
return `${value}`;
}

export function mapMatomoAggregationTypeToLooker(matomoAggregation: string): AggregationType|undefined {
switch (matomoAggregation.toLowerCase()) {
case 'avg':
return AggregationType.AVG;
case 'count': // TODO: do we really need to support count and count_distinct? does not seem to apply to matomo
return AggregationType.COUNT;
case 'count_distinct':
return AggregationType.COUNT_DISTINCT;
case 'max':
return AggregationType.SUM;
case 'min':
return AggregationType.MIN;
case 'sum':
return AggregationType.SUM;
default:
log(`unknown matomo aggregation type encountered: ${matomoAggregation}`);
return undefined;
}
}
27 changes: 22 additions & 5 deletions src/schema/report-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as Api from '../api';
import {
DATE_DIMENSIONS,
mapMatomoSemanticTypeToLooker,
mapMatomoAggregationTypeToLooker,
} from "./data-types";

export function getReportMetadataAndGoalsAndCurrency(request: GoogleAppsScript.Data_Studio.Request<ConnectorParams>) {
Expand Down Expand Up @@ -87,15 +88,28 @@ function metricsForEachGoal(metrics: Record<string, string>, goals: Record<strin
return perGoalMetrics;
}

function addMetric(fields: GoogleAppsScript.Data_Studio.Fields, id: string, name: string, matomoType: string, siteCurrency: string) {
function addMetric(
fields: GoogleAppsScript.Data_Studio.Fields,
id: string,
name: string,
matomoType: string,
siteCurrency: string,
reaggregation?: string,
) {
let type = mapMatomoSemanticTypeToLooker(matomoType, siteCurrency);
let aggregationType = mapMatomoAggregationTypeToLooker(reaggregation);

fields
const field = fields
.newMetric()
.setId(id)
.setName(name)
.setType(type)
.setIsReaggregatable(false);
.setType(type);

if (aggregationType) {
field.setAggregation(aggregationType);
} else {
field.setIsReaggregatable(false);
}
}

function addDimension(fields: GoogleAppsScript.Data_Studio.Fields, id: string, dimension: string) {
Expand Down Expand Up @@ -209,16 +223,19 @@ export function getFieldsFromReportMetadata(

if (allMetrics[metricId]) {
let matomoType: string;
let aggregationType: string;

const m = metricId.match(/^goal_\d+_(.*)/)
if (m) {
matomoType = reportMetadata.metricTypesGoal?.[m[1]];
aggregationType = reportMetadata.metricAggregationTypesGoal?.[m[1]]; // TODO: handle in core
} else {
matomoType = reportMetadata.metricTypes?.[metricId];
aggregationType = reportMetadata.metricAggregationTypes?.[metricId];
}
matomoType = matomoType || 'text';

addMetric(fields, metricId, allMetrics[metricId], matomoType, siteCurrency);
addMetric(fields, metricId, allMetrics[metricId], matomoType, siteCurrency, aggregationType);
} else if (metricId === 'nb_uniq_visitors') {
// to support showing nb_uniq_visitors for day periods, but not others, we need to make sure
// the metric appears in the schema no matter what date range is required. which means adding
Expand Down

0 comments on commit 7eb853b

Please sign in to comment.