Skip to content

Instantly share code, notes, and snippets.

@kemsakurai
Last active July 7, 2019 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kemsakurai/8e8217486cbd01fd521cededefe112f4 to your computer and use it in GitHub Desktop.
Save kemsakurai/8e8217486cbd01fd521cededefe112f4 to your computer and use it in GitHub Desktop.

puppeteerwebpagetest

Google Cloud Functions で、pupeteer を実行し、ページのパフォーマンス指標を取得、結果を Google スプレッドシートに転記する GAS と Cloud Function。

取得するデータ

  • window.performance.timing

  • FirstMeaningfulPaint

  • Paint Timing API (FC FCP)

  • Resource Timing API

各ファイルの説明

const puppeteer = require('puppeteer');
// リクエストヘッダーからTokenをパース
const getAccessToken = function(header) {
if (!header) return null;
const match = header.match(/^Bearer\s+([^\s]+)$/);
return match ? match[1] : null;
}
async function waitForFMP(page) {
let doneMet = null
while (true) {
const data = await getPerformanceMetrics(page)
if (data.FirstMeaningfulPaint !== 0) {
doneMet = data
break
}
await new Promise(resolve => setTimeout(resolve, 300))
}
return doneMet
}
// メインで実行したい関数
async function authorized(req, res) {
const url = req.body.url;
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
let headers;
page.on('response', response => {
if (response.url() == url) {
headers = response.headers();
}
});
const client = await page.target().createCDPSession();
await client.send('Performance.enable');
await page.goto(url);
// performanceMetrics
const performanceMetrics = await waitForFMP(page);
let result = new Object();
result.performanceMetrics = performanceMetrics;
// performanceTiming
const performanceTiming = JSON.parse(
await page.evaluate(() => JSON.stringify(window.performance.timing))
);
result.performanceTiming = performanceTiming;
// resourceTiming
const resourceTiming = JSON.parse(
await page.evaluate(() => JSON.stringify(window.performance.getEntriesByType("resource")))
);
result.resourceTiming = resourceTiming;
result.headers = headers;
// paintTiming
const paintTiming = JSON.parse(
await page.evaluate(() => JSON.stringify(window.performance.getEntriesByType('paint')))
);
result.paintTiming = paintTiming;
result.timestamp = new Date();
await browser.close();
res.status(200).send(JSON.stringify(result));
}
async function getPerformanceMetrics(page) {
const { metrics } = await page._client.send('Performance.getMetrics')
return metrics.reduce((acc, i) => ({ ...acc, [i.name]: i.value }), {})
}
/**
* Responds to any HTTP request.
* メイン関数を認証フローでラップした関数
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.run = function(req, res) {
const accessToken = getAccessToken(req.get('Authorization'));
if ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" == accessToken) {
authorized(req, res);
} else {
res.status(403).send("不正なアクセスです");
}
};
var URL = "https://www.monotalk.xyz/";
function runWebPageTest() {
var response = fetch_(URL);
populatePerformance_(response);
pupulateResource_(response);
}
{
"name": "puppeteerwebpagetest",
"version": "0.0.1",
"description": "Test website performance with Puppeteer on Cloud Function ",
"main": "index.js",
"scripts": {
"test": "test"
},
"keywords": [
"puppeteer"
],
"author": "K.Sakurai",
"license": "MIT",
"dependencies": {
"puppeteer": "^1.18.1"
}
}
{
"performanceMetrics": {
"Timestamp": 19.390782,
"AudioHandlers": 0,
"Documents": 10,
"Frames": 10,
"JSEventListeners": 72,
"LayoutObjects": 1606,
"MediaKeySessions": 0,
"MediaKeys": 0,
"Nodes": 1491,
"Resources": 104,
"ScriptPromises": 0,
"ContextLifecycleStateObservers": 0,
"V8PerContextDatas": 18,
"WorkerGlobalScopes": 1,
"UACSSResources": 0,
"RTCPeerConnections": 0,
"ResourceFetchers": 12,
"AdSubframes": 0,
"DetachedScriptStates": 4,
"LayoutCount": 20,
"RecalcStyleCount": 34,
"LayoutDuration": 3.551115,
"RecalcStyleDuration": 0.665587,
"ScriptDuration": 4.473323,
"V8CompileDuration": 0.029408,
"TaskDuration": 9.679491,
"TaskOtherDuration": 0.989466,
"ThreadTime": 4.14,
"JSHeapUsedSize": 14390760,
"JSHeapTotalSize": 26738688,
"FirstMeaningfulPaint": 16.92774,
"DomContentLoaded": 9.387808,
"NavigationStart": 8.281774
},
"performanceTiming": {
"navigationStart": 1562279482600,
"unloadEventStart": 0,
"unloadEventEnd": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1562279482613,
"domainLookupStart": 1562279482661,
"domainLookupEnd": 1562279482973,
"connectStart": 1562279482973,
"connectEnd": 1562279483324,
"secureConnectionStart": 1562279483098,
"requestStart": 1562279483328,
"responseStart": 1562279483480,
"responseEnd": 1562279483626,
"domLoading": 1562279483517,
"domInteractive": 1562279483706,
"domContentLoadedEventStart": 1562279483706,
"domContentLoadedEventEnd": 1562279483706,
"domComplete": 1562279492642,
"loadEventStart": 1562279492643,
"loadEventEnd": 1562279492662
},
"resourceTiming": [
{
"name": "https://www.monotalk.xyz/static/img/home-bg-1600.jpg",
"entryType": "resource",
"startTime": 986.3999999999997,
"duration": 462.3550000000006,
"initiatorType": "link",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 986.3999999999997,
"domainLookupStart": 986.3999999999997,
"domainLookupEnd": 986.3999999999997,
"connectStart": 986.3999999999997,
"connectEnd": 986.3999999999997,
"secureConnectionStart": 986.3999999999997,
"requestStart": 1046.9299999999996,
"responseStart": 1197.6499999999994,
"responseEnd": 1448.7550000000003,
"transferSize": 129142,
"encodedBodySize": 128123,
"decodedBodySize": 128123,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/pjax-f06b8bea021cc2d24c67.js",
"entryType": "resource",
"startTime": 1056.4399999999985,
"duration": 519.2850000000001,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1056.4399999999985,
"domainLookupStart": 1056.4399999999985,
"domainLookupEnd": 1056.4399999999985,
"connectStart": 1056.4399999999985,
"connectEnd": 1056.4399999999985,
"secureConnectionStart": 1056.4399999999985,
"requestStart": 1162.255,
"responseStart": 1496.8799999999992,
"responseEnd": 1575.7249999999985,
"transferSize": 38307,
"encodedBodySize": 37977,
"decodedBodySize": 139340,
"serverTiming": []
},
{
"name": "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js",
"entryType": "resource",
"startTime": 1059.0849999999996,
"duration": 221.74500000000035,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1059.0849999999996,
"domainLookupStart": 1162.939999999999,
"domainLookupEnd": 1212.4349999999993,
"connectStart": 1212.4349999999993,
"connectEnd": 1235.1349999999998,
"secureConnectionStart": 1218.8,
"requestStart": 1235.7999999999993,
"responseStart": 1250.9549999999995,
"responseEnd": 1280.83,
"transferSize": 34846,
"encodedBodySize": 34385,
"decodedBodySize": 93182,
"serverTiming": []
},
{
"name": "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML",
"entryType": "resource",
"startTime": 1060.6650000000002,
"duration": 309.13499999999954,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1060.6650000000002,
"domainLookupStart": 1163.29,
"domainLookupEnd": 1212.6649999999995,
"connectStart": 1212.6649999999995,
"connectEnd": 1324.7799999999988,
"secureConnectionStart": 1224.2799999999984,
"requestStart": 1325.3050000000003,
"responseStart": 1360.384999999999,
"responseEnd": 1369.7999999999997,
"transferSize": 19079,
"encodedBodySize": 18682,
"decodedBodySize": 63532,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/vendor-f06b8bea021cc2d24c67.js",
"entryType": "resource",
"startTime": 1060.744999999999,
"duration": 435.61500000000024,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1060.744999999999,
"domainLookupStart": 1060.744999999999,
"domainLookupEnd": 1060.744999999999,
"connectStart": 1060.744999999999,
"connectEnd": 1060.744999999999,
"secureConnectionStart": 1060.744999999999,
"requestStart": 1165.1499999999987,
"responseStart": 1485.2099999999987,
"responseEnd": 1496.3599999999992,
"transferSize": 30135,
"encodedBodySize": 29839,
"decodedBodySize": 85431,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/bundle-f06b8bea021cc2d24c67.js",
"entryType": "resource",
"startTime": 1060.8449999999987,
"duration": 559.5500000000015,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1060.8449999999987,
"domainLookupStart": 1060.8449999999987,
"domainLookupEnd": 1060.8449999999987,
"connectStart": 1060.8449999999987,
"connectEnd": 1060.8449999999987,
"secureConnectionStart": 1060.8449999999987,
"requestStart": 1165.31,
"responseStart": 1578.36,
"responseEnd": 1620.3950000000002,
"transferSize": 45007,
"encodedBodySize": 44632,
"decodedBodySize": 143344,
"serverTiming": []
},
{
"name": "https://www.googletagmanager.com/gtm.js?id=GTM-534QXXZ",
"entryType": "resource",
"startTime": 1084.2200000000003,
"duration": 181.86499999999842,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1084.2200000000003,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 1266.0849999999987,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/report/",
"entryType": "resource",
"startTime": 1100.9199999999985,
"duration": 533.7700000000007,
"initiatorType": "violationreport",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1100.9199999999985,
"domainLookupStart": 1100.9199999999985,
"domainLookupEnd": 1100.9199999999985,
"connectStart": 1100.9199999999985,
"connectEnd": 1100.9199999999985,
"secureConnectionStart": 1100.9199999999985,
"requestStart": 1220.1050000000002,
"responseStart": 1629.835,
"responseEnd": 1634.6899999999991,
"transferSize": 150,
"encodedBodySize": 51,
"decodedBodySize": 39,
"serverTiming": [
{
"name": "res",
"duration": 136,
"description": "Server app response time"
}
]
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/bundle-f06b8bea021cc2d24c67.css",
"entryType": "resource",
"startTime": 1104.6399999999999,
"duration": 390.65499999999884,
"initiatorType": "link",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1104.6399999999999,
"domainLookupStart": 1104.6399999999999,
"domainLookupEnd": 1104.6399999999999,
"connectStart": 1104.6399999999999,
"connectEnd": 1104.6399999999999,
"secureConnectionStart": 1104.6399999999999,
"requestStart": 1221.8199999999993,
"responseStart": 1489.5699999999988,
"responseEnd": 1495.2949999999987,
"transferSize": 8186,
"encodedBodySize": 8067,
"decodedBodySize": 58079,
"serverTiming": []
},
{
"name": "https://fonts.gstatic.com/s/opensans/v15/mem5YaGs126MiZpBA-UN8rsOUuhpKKSTjw.woff2",
"entryType": "resource",
"startTime": 1351.3099999999997,
"duration": 3125.115,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1351.3099999999997,
"domainLookupStart": 4417.204999999999,
"domainLookupEnd": 4427.709999999999,
"connectStart": 4427.709999999999,
"connectEnd": 4449.425,
"secureConnectionStart": 4434.865000000001,
"requestStart": 4451.435,
"responseStart": 4475.875,
"responseEnd": 4476.424999999999,
"transferSize": 9040,
"encodedBodySize": 8924,
"decodedBodySize": 8924,
"serverTiming": []
},
{
"name": "https://fonts.gstatic.com/s/opensans/v15/mem8YaGs126MiZpBA-UFVZ0bf8pkAg.woff2",
"entryType": "resource",
"startTime": 1473.7650000000003,
"duration": 3001.92,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1473.7650000000003,
"domainLookupStart": 1473.7650000000003,
"domainLookupEnd": 1473.7650000000003,
"connectStart": 1473.7650000000003,
"connectEnd": 1473.7650000000003,
"secureConnectionStart": 1473.7650000000003,
"requestStart": 4451.609999999999,
"responseStart": 4466.405,
"responseEnd": 4475.685,
"transferSize": 9144,
"encodedBodySize": 8892,
"decodedBodySize": 8892,
"serverTiming": []
},
{
"name": "https://fonts.gstatic.com/s/lora/v12/0QIvMX1D_JOuMwr7I_FMl_E.woff2",
"entryType": "resource",
"startTime": 1665.4549999999997,
"duration": 2886.084999999999,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 1665.4549999999997,
"domainLookupStart": 1665.4549999999997,
"domainLookupEnd": 1665.4549999999997,
"connectStart": 1665.4549999999997,
"connectEnd": 1665.4549999999997,
"secureConnectionStart": 1665.4549999999997,
"requestStart": 4478.79,
"responseStart": 4536.525,
"responseEnd": 4551.539999999999,
"transferSize": 15409,
"encodedBodySize": 15292,
"decodedBodySize": 15292,
"serverTiming": []
},
{
"name": "https://fonts.gstatic.com/s/lora/v12/0QIhMX1D_JOuMw_LIftLtfOm8w.woff2",
"entryType": "resource",
"startTime": 2310.81,
"duration": 2244.275,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 2310.81,
"domainLookupStart": 2310.81,
"domainLookupEnd": 2310.81,
"connectStart": 2310.81,
"connectEnd": 2310.81,
"secureConnectionStart": 2310.81,
"requestStart": 4478.994999999999,
"responseStart": 4551.700000000001,
"responseEnd": 4555.085,
"transferSize": 16929,
"encodedBodySize": 16832,
"decodedBodySize": 16832,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/report/",
"entryType": "resource",
"startTime": 4345.67,
"duration": 611.6549999999988,
"initiatorType": "violationreport",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4345.67,
"domainLookupStart": 4345.67,
"domainLookupEnd": 4345.67,
"connectStart": 4345.67,
"connectEnd": 4345.67,
"secureConnectionStart": 4345.67,
"requestStart": 4555.32,
"responseStart": 4947.794999999999,
"responseEnd": 4957.324999999999,
"transferSize": 2198,
"encodedBodySize": 51,
"decodedBodySize": 39,
"serverTiming": [
{
"name": "res",
"duration": 122,
"description": "Server app response time"
}
]
},
{
"name": "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-MML-AM_CHTML.js?V=2.7.5",
"entryType": "resource",
"startTime": 4346.05,
"duration": 268.3649999999989,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4346.05,
"domainLookupStart": 4346.05,
"domainLookupEnd": 4346.05,
"connectStart": 4346.05,
"connectEnd": 4346.05,
"secureConnectionStart": 4346.05,
"requestStart": 4556.334999999999,
"responseStart": 4590.665,
"responseEnd": 4614.414999999999,
"transferSize": 73802,
"encodedBodySize": 73567,
"decodedBodySize": 280478,
"serverTiming": []
},
{
"name": "https://adservice.google.com/adsid/integrator.js?domain=www.monotalk.xyz",
"entryType": "resource",
"startTime": 4659.3949999999995,
"duration": 287.5099999999993,
"initiatorType": "link",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4659.3949999999995,
"domainLookupStart": 4715.384999999999,
"domainLookupEnd": 4729.3099999999995,
"connectStart": 4729.3099999999995,
"connectEnd": 4797.724999999999,
"secureConnectionStart": 4735.3899999999985,
"requestStart": 4798.689999999999,
"responseStart": 4920.1449999999995,
"responseEnd": 4946.904999999999,
"transferSize": 476,
"encodedBodySize": 104,
"decodedBodySize": 109,
"serverTiming": []
},
{
"name": "https://pagead2.googlesyndication.com/pagead/js/r20190626/r20190131/show_ads_impl.js",
"entryType": "resource",
"startTime": 4668.804999999999,
"duration": 80.07999999999993,
"initiatorType": "link",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4668.804999999999,
"domainLookupStart": 4668.804999999999,
"domainLookupEnd": 4668.804999999999,
"connectStart": 4668.804999999999,
"connectEnd": 4668.804999999999,
"secureConnectionStart": 4668.804999999999,
"requestStart": 4719.804999999999,
"responseStart": 4739.65,
"responseEnd": 4748.884999999999,
"transferSize": 80503,
"encodedBodySize": 80308,
"decodedBodySize": 216830,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/e6cf7c6ec7c2d6f670ae9d762604cb0b.woff2",
"entryType": "resource",
"startTime": 4853.265,
"duration": 2640.67,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4853.265,
"domainLookupStart": 4853.265,
"domainLookupEnd": 4853.265,
"connectStart": 4853.265,
"connectEnd": 4853.265,
"secureConnectionStart": 4853.265,
"requestStart": 7267.199999999999,
"responseStart": 7493.045,
"responseEnd": 7493.935,
"transferSize": 1490,
"encodedBodySize": 1423,
"decodedBodySize": 1400,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/static/webpack_bundles/448c34a56d699c29117adc64c43affeb.woff2",
"entryType": "resource",
"startTime": 4859.169999999999,
"duration": 2633.6600000000008,
"initiatorType": "css",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4859.169999999999,
"domainLookupStart": 4859.169999999999,
"domainLookupEnd": 4859.169999999999,
"connectStart": 4859.169999999999,
"connectEnd": 4859.169999999999,
"secureConnectionStart": 4859.169999999999,
"requestStart": 7267.809999999999,
"responseStart": 7486.634999999999,
"responseEnd": 7492.83,
"transferSize": 1322,
"encodedBodySize": 1147,
"decodedBodySize": 1124,
"serverTiming": []
},
{
"name": "https://pagead2.googlesyndication.com/pub-config/r20160913/ca-pub-6873737391821354.js",
"entryType": "resource",
"startTime": 4887.345,
"duration": 265.7049999999999,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 4887.345,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 5153.05,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
},
{
"name": "https://googleads.g.doubleclick.net/pagead/html/r20190626/r20190131/zrt_lookup.html#",
"entryType": "resource",
"startTime": 5232.424999999999,
"duration": 303.2350000000006,
"initiatorType": "iframe",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 5232.424999999999,
"domainLookupStart": 5347.199999999999,
"domainLookupEnd": 5399.094999999999,
"connectStart": 5399.094999999999,
"connectEnd": 5471.95,
"secureConnectionStart": 5401.17,
"requestStart": 5473.110000000001,
"responseStart": 5503.135,
"responseEnd": 5535.66,
"transferSize": 7453,
"encodedBodySize": 7010,
"decodedBodySize": 16471,
"serverTiming": []
},
{
"name": "https://www.google-analytics.com/analytics.js",
"entryType": "resource",
"startTime": 6678.110000000001,
"duration": 581.5999999999995,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 6678.110000000001,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 7259.71,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
},
{
"name": "https://www.monotalk.xyz/report/",
"entryType": "resource",
"startTime": 7636.435,
"duration": 425.6700000000001,
"initiatorType": "violationreport",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 7636.435,
"domainLookupStart": 7636.435,
"domainLookupEnd": 7636.435,
"connectStart": 7636.435,
"connectEnd": 7636.435,
"secureConnectionStart": 7636.435,
"requestStart": 7738.1849999999995,
"responseStart": 8048.1449999999995,
"responseEnd": 8062.1050000000005,
"transferSize": 173,
"encodedBodySize": 51,
"decodedBodySize": 39,
"serverTiming": [
{
"name": "res",
"duration": 112,
"description": "Server app response time"
}
]
},
{
"name": "https://www.googletagservices.com/activeview/js/current/osd.js?cb=%2Fr20100101",
"entryType": "resource",
"startTime": 7636.92,
"duration": 252.27000000000135,
"initiatorType": "script",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 7636.92,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 7889.190000000001,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
},
{
"name": "https://ampcid.google.com/v1/publisher:getClientId?key=AIzaSyA65lEHUEizIsNtlbNo-l2K18dT680nsaM",
"entryType": "resource",
"startTime": 8662.854999999998,
"duration": 87.94500000000153,
"initiatorType": "xmlhttprequest",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 8662.854999999998,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 8750.8,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
},
{
"name": "https://www.google-analytics.com/r/collect?v=1&_v=j77&a=907263970&t=pageview&_s=1&dl=https%3A%2F%2Fwww.monotalk.xyz%2F&ul=en-us&de=UTF-8&dt=%E3%83%96%E3%83%AD%E3%82%B0%20%7C%20Monotalk&sd=24-bit&sr=800x600&vp=800x600&je=0&_u=YAhAAEABAAQC~&jid=201754943&gjid=1401238284&cid=1813473390.1562279490&tid=UA-63621442-1&_gid=1575824815.1562279492&_r=1&gtm=2wg6k2534QXXZ&cd9=Mozilla%2F5.0%20(X11%3B%20Linux%20x86_64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20HeadlessChrome%2F77.0.3835.0%20Safari%2F537.36&cd10=2019-07-05%2007%3A31%3A29&cd12=NVIGATE&cd13=0&cd14=10&cd15=UNSUPPORTED&cd16=4g&cd17=UNSUPPORTED&cd18=0&cd19=UNSUPPORTED&cd20=16&cd8=1813473390.1562279490&z=148900632",
"entryType": "resource",
"startTime": 9942.52,
"duration": 57.89499999999862,
"initiatorType": "img",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 9942.52,
"domainLookupStart": 9946.795,
"domainLookupEnd": 9957.904999999999,
"connectStart": 9957.904999999999,
"connectEnd": 9973.789999999999,
"secureConnectionStart": 9959.335000000001,
"requestStart": 9974.839999999998,
"responseStart": 9987.615,
"responseEnd": 10000.414999999999,
"transferSize": 374,
"encodedBodySize": 42,
"decodedBodySize": 42,
"serverTiming": []
},
{
"name": "https://www.google-analytics.com/collect?v=1&_v=j77&a=907263970&t=event&ni=1&_s=1&dl=https%3A%2F%2Fwww.monotalk.xyz%2F&ul=en-us&de=UTF-8&dt=%E3%83%96%E3%83%AD%E3%82%B0%20%7C%20Monotalk&sd=24-bit&sr=800x600&vp=800x600&je=0&ec=Scroll&ea=%2F&el=0%25&_u=aAjAAEABAAQC~&jid=&gjid=&cid=1813473390.1562279490&tid=UA-63621442-1&_gid=1575824815.1562279492&gtm=2wg6k2534QXXZ&cd9=Mozilla%2F5.0%20(X11%3B%20Linux%20x86_64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20HeadlessChrome%2F77.0.3835.0%20Safari%2F537.36&cd10=2019-07-05%2007%3A31%3A32&cd12=NVIGATE&cd13=0&cd14=10&cd15=UNSUPPORTED&cd16=4g&cd17=UNSUPPORTED&cd18=0&cd19=UNSUPPORTED&cd20=16&cd8=1813473390.1562279490&z=742424082",
"entryType": "resource",
"startTime": 10424.415,
"duration": 119.95000000000073,
"initiatorType": "img",
"nextHopProtocol": "h2",
"workerStart": 0,
"redirectStart": 0,
"redirectEnd": 0,
"fetchStart": 10424.415,
"domainLookupStart": 0,
"domainLookupEnd": 0,
"connectStart": 0,
"connectEnd": 0,
"secureConnectionStart": 0,
"requestStart": 0,
"responseStart": 0,
"responseEnd": 10544.365000000002,
"transferSize": 0,
"encodedBodySize": 0,
"decodedBodySize": 0,
"serverTiming": []
}
],
"headers": {
"status": "200",
"date": "Thu, 04 Jul 2019 22:31:23 GMT",
"server": "Apache",
"set-cookie": "track=2a4a7bd1.58ce289a5b609; path=/; max-age=63072000; domain=.monotalk.xyz\ncsrftoken=za1C03U1tNhfQItxKFMkI4hIUq5wLKYI5iWvBHOxT1dVoGI9qnW16eKFY8fd0xTb; expires=Thu, 02-Jul-2020 22:31:23 GMT; Max-Age=31449600; Path=/; SameSite=lax; Secure",
"referrer-policy": "strict-origin-when-cross-origin",
"feature-policy": "accelerometer 'none'; ambient-light-sensor 'none'; autoplay 'none'; camera 'none'; encrypted-media 'none'; fullscreen 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none';microphone 'none'; midi 'none';payment 'none';picture-in-picture 'none';speaker 'none';usb 'none';vr 'none'",
"pragma": "no-cache",
"cache-control": "no-cache, no-store, must-revalidate, private",
"content-security-policy-report-only": "script-src 'self' 'unsafe-inline' 'unsafe-eval' pagead2.googlesyndication.com www.google-analytics.com *.disqus.com c.disquscdn.com disqus.com *.googletagmanager.com *.google-analytics.com *.ampproject.org adservice.google.com tagmanager.google.com adservice.google.co.jp uh.nakanohito.jp platform.twitter.com cdn.syndication.twimg.com tagmanager.google.com googleads.g.doubleclick.net adservice.google.ca adservice.google.co.in adservice.google.co.kr adservice.google.co.th adservice.google.co.uk adservice.google.com.au adservice.google.com.br adservice.google.com.hk adservice.google.com.mm adservice.google.com.my adservice.google.com.ph adservice.google.com.sg adservice.google.com.tw adservice.google.com.vn adservice.google.de adservice.google.es adservice.google.fr cdn.mxpnl.com kitchen.juicer.cc; style-src 'self' 'unsafe-inline' *.disquscdn.com *.google.com *.googleapis.com *.monotalk.xyz platform.twitter.com ton.twimg.com; connect-src 'self' data: *.disqus.com uh0.nakanohito.jp www.google-analytics.com pagead2.googlesyndication.com syndication.twitter.com 3p.ampproject.net www.googletagmanager.com api.mixpanel.com ampcid.google.com ampcid.google.co.jp *.ampproject.net stats.g.doubleclick.net csi.gstatic.com kitchen.juicer.cc; default-src 'self' c.disquscdn.com disqus.com disqusads.com googleads.g.doubleclick.net; img-src 'self' *.disqus.com *.disquscdn.com *.googleusercontent.com *.google.com *.googledrive.comgoogledrive.com data: googledrive.com drive.google.com www.gstatic.com www.monotalk.xyz ssl.gstatic.com ir-jp.amazon-adsystem.com ssl.google-analytics.com www.google-analytics.com drive.google.com pagead2.googlesyndication.com stats.g.doubleclick.net cdn.viglink.com bcp.crwdcntrl.net drive.google.com www.googletagmanager.com; child-src 'self' googleads.g.doubleclick.net staticxx.facebook.com *.disqus.com kemsakurai.github.io *.ampproject.net syndication.twitter.com platform.twitter.com www.googletagmanager.com rcm-fe.amazon-adsystem.com disqus.com disqusads.com rcm-fe.amazon-adsystem.com; font-src 'self' fonts.gstatic.com *.monotalk.xyz data:; object-src pagead2.googlesyndication.com; report-uri /report/",
"x-frame-options": "DENY",
"vary": "Cookie,Accept-Language,Accept-Encoding",
"content-language": "ja",
"x-api-discovery": "https://www.monotalk.xyz/xyz_monotalk_api/",
"strict-transport-security": "max-age=63072000; includeSubDomains; preload",
"x-content-type-options": "nosniff",
"x-xss-protection": "1; mode=block",
"server-timing": "res;desc=\"Server app response time\";dur=16",
"content-encoding": "gzip",
"content-type": "text/html; charset=utf-8"
},
"paintTiming": [
{
"name": "first-paint",
"entryType": "paint",
"startTime": 8645.964999999998,
"duration": 0
},
{
"name": "first-contentful-paint",
"entryType": "paint",
"startTime": 8645.964999999998,
"duration": 0
}
],
"timestamp": "2019-07-04T22:31:33.963Z"
}
function fetch_(url) {
var headers = {
'Authorization': 'Bearer xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
};
var data = {
url: url
};
var options = {
"method" : "post",
"payload" : data,
"headers" : headers,
"muteHttpExceptions": false
};
var result = UrlFetchApp.fetch('https://cloudfunction/url', options);
return JSON.parse(result.getContentText());
}
function pupulateResource_(reposonse) {
// -----------------------------------------------
// シートオブジェクトを取得する
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('resource');
var resourceTiming = reposonse.resourceTiming;
var values = new Array();
for (var i = 0; i < resourceTiming.length; i++) {
var elems = new Array();
elems.push(reposonse.timestamp);
elems.push(resourceTiming[i].name);
// hostname
elems.push(extractHostname_(resourceTiming[i].name));
elems.push(resourceTiming[i].entryType);
elems.push(resourceTiming[i].startTime);
elems.push(resourceTiming[i].duration);
elems.push(resourceTiming[i].initiatorType);
elems.push(resourceTiming[i].nextHopProtocol);
elems.push(resourceTiming[i].workerStart);
elems.push(resourceTiming[i].redirectStart);
elems.push(resourceTiming[i].redirectEnd);
elems.push(resourceTiming[i].fetchStart);
elems.push(resourceTiming[i].domainLookupStart);
elems.push(resourceTiming[i].domainLookupEnd);
elems.push(resourceTiming[i].connectStart);
elems.push(resourceTiming[i].connectEnd);
elems.push(resourceTiming[i].secureConnectionStart);
elems.push(resourceTiming[i].requestStart);
elems.push(resourceTiming[i].responseStart);
elems.push(resourceTiming[i].responseEnd);
elems.push(resourceTiming[i].transferSize);
elems.push(resourceTiming[i].encodedBodySize);
elems.push(resourceTiming[i].decodedBodySize);
values.push(elems);
}
var range = sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length);
range.setValues(values);
}
function populatePerformance_(response) {
// -----------------------------------------------
// シートオブジェクトを取得する
// -----------------------------------
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('performance');
// -----------------------------------------------
// シートに記録する値を、設定
// -----------------------------------
var values = new Array();
// ------------------
// timestamp
values.push(response.timestamp);
// ------------------
// performanceMetrics
// Frames
values.push(response.performanceMetrics.Frames);
// JSEventListeners
values.push(response.performanceMetrics.JSEventListeners);
// LayoutObjects
values.push(response.performanceMetrics.LayoutObjects);
// Nodes
values.push(response.performanceMetrics.Nodes);
// Resources
values.push(response.performanceMetrics.Resources);
// V8PerContextDatas
values.push(response.performanceMetrics.V8PerContextDatas);
// RecalcStyleCount
values.push(response.performanceMetrics.RecalcStyleCount);
// LayoutDuration
values.push(response.performanceMetrics.LayoutDuration);
// RecalcStyleDuration
values.push(response.performanceMetrics.RecalcStyleDuration);
// ScriptDuration
values.push(response.performanceMetrics.ScriptDuration);
// TaskDuration
values.push(response.performanceMetrics.TaskDuration);
// TaskOtherDuration
values.push(response.performanceMetrics.TaskOtherDuration);
// ThreadTime
values.push(response.performanceMetrics.ThreadTime);
// JSHeapUsedSize
values.push(response.performanceMetrics.JSHeapUsedSize);
// JSHeapTotalSize
values.push(response.performanceMetrics.JSHeapTotalSize);
// FirstMeaningfulPaint
values.push(response.performanceMetrics.FirstMeaningfulPaint - response.performanceMetrics.NavigationStart);
// DomContentLoaded
values.push(response.performanceMetrics.FirstMeaningfulPaint - response.performanceMetrics.DomContentLoaded);
// -----------------
// performanceTiming
// performanceTiming.domainLookup
values.push(response.performanceTiming.domainLookupEnd - response.performanceTiming.navigationStart);
// performanceTiming.domInteractive
values.push(response.performanceTiming.domInteractive - response.performanceTiming.navigationStart);
// performanceTiming.domContentLoadedEventEnd
values.push(response.performanceTiming.domContentLoadedEventEnd - response.performanceTiming.navigationStart);
// performanceTiming.domComplete
values.push(response.performanceTiming.domInteractive - response.performanceTiming.navigationStart);
// loadEventEnd
values.push(response.performanceTiming.loadEventEnd - response.performanceTiming.navigationStart);
// response.paintTiming.startTime first-paint
values.push(response.paintTiming[0].startTime);
// response.paintTiming.startTime first-contentful-paint
values.push(response.paintTiming[1].startTime);
var range = sheet.getRange(sheet.getLastRow() + 1, 1, 1, values.length);
range.setValues([values]);
}
// host名切り出し
function extractHostname_(url) {
var hostname;
//find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("//") > -1) {
hostname = url.split('/')[2];
}
else {
hostname = url.split('/')[0];
}
//find & remove port number
hostname = hostname.split(':')[0];
//find & remove "?"
hostname = hostname.split('?')[0];
return hostname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment