@adobe/spacecat-shared-athena-client 1.2.0 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-athena-client-v1.2.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-athena-client-v1.2.1...@adobe/spacecat-shared-athena-client-v1.2.2) (2025-07-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * switch to js template ([#876](https://github.com/adobe/spacecat-shared/issues/876)) ([4e843c6](https://github.com/adobe/spacecat-shared/commit/4e843c66a0d5b0d7d8652c03bb184e9f4253e4ea))
7
+
8
+ # [@adobe/spacecat-shared-athena-client-v1.2.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-athena-client-v1.2.0...@adobe/spacecat-shared-athena-client-v1.2.1) (2025-07-24)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * mapping of traffic data response ([#871](https://github.com/adobe/spacecat-shared/issues/871)) ([ca65936](https://github.com/adobe/spacecat-shared/commit/ca659361b2f61c83bf995c285860d479e9f82066))
14
+
1
15
  # [@adobe/spacecat-shared-athena-client-v1.2.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-athena-client-v1.1.1...@adobe/spacecat-shared-athena-client-v1.2.0) (2025-07-24)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-athena-client",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Shared modules of the Spacecat Services - AWS Athena Client",
5
5
  "type": "module",
6
6
  "engines": {
@@ -9,33 +9,34 @@
9
9
  * OF ANY KIND, either express or implied. See the License for the specific language
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
- import { getStaticContent, getDateRanges } from '@adobe/spacecat-shared-utils';
13
- import path from 'path';
14
- import { fileURLToPath } from 'url';
15
-
16
- const currentFile = fileURLToPath(import.meta.url);
17
- const currentDir = path.dirname(currentFile);
18
-
19
- const TRAFFIC_ANALYSIS_PATH = path.resolve(currentDir, '../../static/queries/traffic-analysis.sql.tpl');
12
+ import { getDateRanges } from '@adobe/spacecat-shared-utils';
13
+ import { getTrafficAnalysisTemplate } from './traffic-analysis-template.js';
20
14
 
21
15
  /**
22
16
  * Loads the traffic analysis query template and applies placeholders.
23
17
  * @param {Object} placeholders - Key-value pairs to replace in the query template.
24
18
  * @param {Object} log - Logger (optional)
25
- * @returns {Promise<string|null>} The templated SQL string or null on error.
19
+ * @returns {string} The templated SQL string.
26
20
  */
27
- export async function getTrafficAnalysisQuery(placeholders = {}) {
28
- return getStaticContent(placeholders, TRAFFIC_ANALYSIS_PATH);
21
+ export function getTrafficAnalysisQuery(placeholders = {}) {
22
+ return getTrafficAnalysisTemplate(placeholders);
29
23
  }
30
24
 
31
25
  /**
32
- * Scans the query template and returns a sorted array of unique placeholder (strings).
33
- * @returns {Promise<string[]>} Array of unique placeholder keys found in the template.
26
+ * Returns a sorted array of unique placeholder keys used in the template.
27
+ * @returns {string[]} Array of unique placeholder keys found in the template.
34
28
  */
35
- export async function getTrafficAnalysisQueryPlaceholders() {
36
- const raw = await getStaticContent({}, TRAFFIC_ANALYSIS_PATH);
37
- const matches = raw.match(/{{\s*([\w]+)\s*}}/g);
38
- return [...new Set((matches).map((m) => m.replace(/{{\s*|\s*}}/g, '')))].sort();
29
+ export function getTrafficAnalysisQueryPlaceholders() {
30
+ // Return the known placeholders used in the template
31
+ return [
32
+ 'dimensionColumns',
33
+ 'dimensionColumnsPrefixed',
34
+ 'groupBy',
35
+ 'pageTypeCase',
36
+ 'siteId',
37
+ 'tableName',
38
+ 'temporalCondition',
39
+ ];
39
40
  }
40
41
 
41
42
  /**
@@ -66,9 +67,9 @@ export function buildPageTypeCase(pageTypes, column) {
66
67
  * @param {number} params.week - The ISO week number (1–53).
67
68
  * @param {number} params.year - The year (e.g. 2025).
68
69
  * @param {string} params.siteId - UUID of the site.
69
- * @param {string[]} [params.dimensions- Dimensions to group by (e.g. ['utm_campaign', 'device']).
70
+ * @param {string[]} [params.dimensions] - Dimensions to group by (e.g. ['utm_campaign', 'device']).
70
71
  * @param {string} params.tableName - The name of the source table.
71
- * @param {string} params.pageTypeMatchColumn - The pageTypeMatchColumn of the source table.
72
+ * @param {string} params.pageTypeMatchColumn - The pageTypeMatchColumn of the source table.
72
73
  * @param {Object|null} [params.pageTypes=null] - Optional pageType rules for CASE generation.
73
74
  *
74
75
  * @returns {Object} Template values for SQL generation.
@@ -0,0 +1,95 @@
1
+ /*
2
+ * Copyright 2025 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /**
14
+ * Generates the traffic analysis SQL query using template literals.
15
+ * @param {Object} params - Template parameters
16
+ * @param {string} params.siteId - Site ID
17
+ * @param {string} params.tableName - Table name
18
+ * @param {string} params.temporalCondition - Temporal condition
19
+ * @param {string} params.dimensionColumns - Dimension columns
20
+ * @param {string} params.groupBy - Group by clause
21
+ * @param {string} params.dimensionColumnsPrefixed - Prefixed dimension columns
22
+ * @param {string} params.pageTypeCase - Page type case statement
23
+ * @returns {string} The SQL query string
24
+ */
25
+ export function getTrafficAnalysisTemplate({
26
+ siteId,
27
+ tableName,
28
+ temporalCondition,
29
+ dimensionColumns,
30
+ groupBy,
31
+ dimensionColumnsPrefixed,
32
+ pageTypeCase,
33
+ }) {
34
+ return `
35
+ WITH raw AS (
36
+ SELECT
37
+ path,
38
+ ${pageTypeCase},
39
+ trf_type,
40
+ trf_channel,
41
+ trf_platform,
42
+ device,
43
+ utm_source,
44
+ utm_medium,
45
+ utm_campaign,
46
+ referrer,
47
+ consent,
48
+ notfound,
49
+ pageviews,
50
+ clicked,
51
+ engaged,
52
+ latest_scroll,
53
+ CASE WHEN latest_scroll >= 10000 THEN 1 ELSE 0 END AS engaged_scroll,
54
+ lcp,
55
+ cls,
56
+ inp
57
+ FROM ${tableName}
58
+ WHERE siteid = '${siteId}'
59
+ AND (${temporalCondition})
60
+ ),
61
+ agg AS (
62
+ SELECT
63
+ ${dimensionColumns},
64
+ COUNT(*) AS row_count,
65
+ CAST(SUM(pageviews) AS BIGINT) AS pageviews,
66
+ CAST(SUM(clicked) AS BIGINT) AS clicks,
67
+ CAST(SUM(engaged) AS BIGINT) AS engagements,
68
+ CAST(SUM(engaged_scroll) AS BIGINT) AS engaged_scroll,
69
+ approx_percentile(latest_scroll, 0.70) AS p70_scroll,
70
+ approx_percentile(lcp, 0.70) AS p70_lcp,
71
+ approx_percentile(cls, 0.70) AS p70_cls,
72
+ approx_percentile(inp, 0.70) AS p70_inp
73
+ FROM raw
74
+ GROUP BY ${groupBy}
75
+ ),
76
+ grand_total AS (
77
+ SELECT CAST(SUM(pageviews) AS BIGINT) AS total_pv FROM agg
78
+ )
79
+ SELECT
80
+ ${dimensionColumnsPrefixed},
81
+ a.pageviews,
82
+ CAST(a.pageviews AS DOUBLE) / NULLIF(t.total_pv, 0) AS pct_pageviews,
83
+ CAST(a.clicks AS DOUBLE) / NULLIF(a.row_count, 0) AS click_rate,
84
+ CAST(a.engagements AS DOUBLE) / NULLIF(a.row_count, 0) AS engagement_rate,
85
+ 1 - CAST(a.engagements AS DOUBLE) / NULLIF(a.row_count, 0) AS bounce_rate,
86
+ a.engaged_scroll,
87
+ a.p70_scroll,
88
+ a.p70_lcp,
89
+ a.p70_cls,
90
+ a.p70_inp
91
+ FROM agg a
92
+ CROSS JOIN grand_total t
93
+ ORDER BY a.pageviews DESC
94
+ `.trim();
95
+ }
@@ -35,7 +35,7 @@ export const TrafficDataResponseDto = {
35
35
  toJSON: (data) => ({
36
36
  type: data.trf_type,
37
37
  channel: data.trf_channel,
38
- platform: data.platform,
38
+ platform: data.trf_platform,
39
39
  utm_source: data.utm_source,
40
40
  utm_medium: data.utm_medium,
41
41
  campaign: data.utm_campaign,
@@ -1,59 +0,0 @@
1
- WITH raw AS (
2
- SELECT
3
- path,
4
- {{pageTypeCase}},
5
- trf_type,
6
- trf_channel,
7
- trf_platform,
8
- device,
9
- utm_source,
10
- utm_medium,
11
- utm_campaign,
12
- referrer,
13
- consent,
14
- notfound,
15
- pageviews,
16
- clicked,
17
- engaged,
18
- latest_scroll,
19
- CASE WHEN latest_scroll >= 10000 THEN 1 ELSE 0 END AS engaged_scroll,
20
- lcp,
21
- cls,
22
- inp
23
- FROM {{tableName}}
24
- WHERE siteid = '{{siteId}}'
25
- AND ({{temporalCondition}})
26
- ),
27
- agg AS (
28
- SELECT
29
- {{dimensionColumns}},
30
- COUNT(*) AS row_count,
31
- CAST(SUM(pageviews) AS BIGINT) AS pageviews,
32
- CAST(SUM(clicked) AS BIGINT) AS clicks,
33
- CAST(SUM(engaged) AS BIGINT) AS engagements,
34
- CAST(SUM(engaged_scroll) AS BIGINT) AS engaged_scroll,
35
- approx_percentile(latest_scroll, 0.70) AS p70_scroll,
36
- approx_percentile(lcp, 0.70) AS p70_lcp,
37
- approx_percentile(cls, 0.70) AS p70_cls,
38
- approx_percentile(inp, 0.70) AS p70_inp
39
- FROM raw
40
- GROUP BY {{groupBy}}
41
- ),
42
- grand_total AS (
43
- SELECT CAST(SUM(pageviews) AS BIGINT) AS total_pv FROM agg
44
- )
45
- SELECT
46
- {{dimensionColumnsPrefixed}},
47
- a.pageviews,
48
- CAST(a.pageviews AS DOUBLE) / NULLIF(t.total_pv, 0) AS pct_pageviews,
49
- CAST(a.clicks AS DOUBLE) / NULLIF(a.row_count, 0) AS click_rate,
50
- CAST(a.engagements AS DOUBLE) / NULLIF(a.row_count, 0) AS engagement_rate,
51
- 1 - CAST(a.engagements AS DOUBLE) / NULLIF(a.row_count, 0) AS bounce_rate,
52
- a.engaged_scroll,
53
- a.p70_scroll,
54
- a.p70_lcp,
55
- a.p70_cls,
56
- a.p70_inp
57
- FROM agg a
58
- CROSS JOIN grand_total t
59
- ORDER BY a.pageviews DESC