-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.gs
90 lines (85 loc) · 2.09 KB
/
main.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function doPost(e)
{
const body = JSON.parse(e.postData.contents);
const query = `
insert into ${table} (
ts,
date,
email,
utm_source,
utm_medium,
utm_campaign,
hashed
)
with x as (
select
timestamp(@ts) as ts,
date(@ts) as date,
@email as email,
@utm_source as utm_source,
@utm_medium as utm_medium,
@utm_campaign as utm_campaign,
sha256(concat(
cast(date(@ts) as string),
cast(@email as string),
cast(@utm_source as string),
cast(@utm_medium as string),
cast(@utm_campaign as string))
) as hashed
)
select
ts,
date,
email,
utm_source,
utm_medium,
utm_campaign,
hashed
from x
where
not exists (
select 1 from ${table} where hashed = x.hashed
)
`;
const { email, utm_source, utm_medium, utm_campaign } = body;
const request = {
useLegacySql: false,
parameterMode: 'NAMED',
query: query,
queryParameters: [
{
name: 'ts',
parameterType: { type: 'TIMESTAMP' },
parameterValue: { value: new Date().toISOString() }
},
{
name: 'email',
parameterType: { type: 'STRING' },
parameterValue: { value: email || '' }
},
{
name: 'utm_source',
parameterType: { type: 'STRING' },
parameterValue: { value: utm_source || '' }
},
{
name: 'utm_medium',
parameterType: { type: 'STRING' },
parameterValue: { value: utm_medium || '' }
},
{
name: 'utm_campaign',
parameterType: { type: 'STRING' },
parameterValue: { value: utm_campaign || '' }
}
]
};
try {
BigQuery.Jobs.query(request, projectId);
return ContentService.createTextOutput(JSON.stringify({ status: 200, message: 'Success' }))
.setMimeType(ContentService.MimeType.JSON);
} catch (err) {
return ContentService.createTextOutput(JSON.stringify({ status: 500, message: err.message }))
.setMimeType(ContentService.MimeType.JSON);
}
}