{ "name": "Email a Google Analytics Comparison Report", "tags": [ { "id": "BimZXo1NKE7JdlXm", "name": "Google Analytics", "createdAt": "2024-11-13T18:08:04.053Z", "updatedAt": "2024-11-13T18:08:04.053Z" }, { "id": "nezaWFCGa7eZsVKu", "name": "Utility", "createdAt": "2024-11-13T18:08:08.207Z", "updatedAt": "2024-11-13T18:08:08.207Z" } ], "nodes": [ { "id": "email-a-google-analytics-comparison-report-0", "name": "When clicking ‘Test workflow’", "type": "n8n-nodes-base.manualTrigger", "position": [ 560, 940 ], "parameters": {}, "typeVersion": 1 }, { "name": "Prepare report dates", "type": "n8n-nodes-base.code", "typeVersion": 2, "parameters": { "mode": "runOnceForAllItems", "jsCode": "const propertyId='YOUR_PROPERTY_ID';if(!/^\\d+$/.test(propertyId))throw Error('Configure a Google Analytics property ID');const day=new Date().toISOString().slice(0,10),d=n=>new Date(Date.parse(day+'T00:00:00Z')+n*86400000).toISOString().slice(0,10);return [{json:{propertyId,day,recentStart:d(-7),recentEnd:d(-1),previousStart:d(-14),previousEnd:d(-8)}}];" }, "id": "email-a-google-analytics-comparison-report-1" }, { "id": "email-a-google-analytics-comparison-report-2", "name": "Get Page Engagement Stats for this week", "type": "n8n-nodes-base.httpRequest", "position": [ 840, 740 ], "parameters": { "method": "POST", "url": "={{ 'https://analyticsdata.googleapis.com/v1beta/properties/' + $('Prepare report dates').first().json.propertyId + ':runReport' }}", "authentication": "predefinedCredentialType", "nodeCredentialType": "googleAnalyticsOAuth2", "sendBody": true, "specifyBody": "json", "jsonBody": "={{ {dateRanges:[{startDate:$('Prepare report dates').first().json.recentStart,endDate:$('Prepare report dates').first().json.recentEnd}],dimensions:[{\"name\":\"unifiedScreenName\"}],metrics:[{\"name\":\"screenPageViews\"},{\"name\":\"activeUsers\"},{\"name\":\"screenPageViewsPerUser\"},{\"name\":\"eventCount\"}],keepEmptyRows:true,limit:10000,offset:0} }}", "options": { "response": { "response": { "responseFormat": "json", "fullResponse": true } }, "pagination": { "pagination": { "paginationMode": "updateAParameterInEachRequest", "parameters": { "parameters": [ { "type": "body", "name": "offset", "value": "={{ $pageCount * 10000 }}" } ] }, "paginationCompleteWhen": "other", "completeExpression": "={{ (() => { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumed { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumed { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumed { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumed { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumed { const b=$response.body;if(!b||typeof b!=='object')throw Error('Invalid analytics page');const rows=b.rows??[],count=b.rowCount??0;if(!Array.isArray(rows)||!Number.isSafeInteger(count)||count<0||rows.length>10000||(!Number.isSafeInteger($pageCount))||$pageCount<1)throw Error('Invalid analytics page count');const consumed=($pageCount-1)*10000+rows.length;if(consumed>count||(consumedString(v).replace(/[&<>\"']/g,c=>({'&':'&','<':'<','>':'>','\"':'"',\"'\":'''}[c]));let html='

Google Analytics report

';\n for(let i=0;i<6;i++){if(!Array.isArray(state.tables[i]))throw Error('Invalid report table');html+='

'+sections[Math.floor(i/2)]+' - '+(i%2?'previous seven days':'latest seven days')+'

';const rows=state.tables[i];if(!rows.length){html+='

No data.

';continue}html+=''+labels[Math.floor(i/2)].map(x=>'').join('')+'';for(const r of rows){if(!Array.isArray(r)||r.length!==labels[Math.floor(i/2)].length)throw Error('Invalid formatted row');html+=''+r.map(x=>'').join('')+''}html+='
'+escape(x)+'
'+escape(x)+'
'}return{to:recipient,subject:'Google Analytics report before '+state.day+' UTC',body:html};\n}\n\nconst names=[\"Get Page Engagement Stats for this week\",\"Get Page Engagement Stats for prior week\",\"Get Google Search Results for this week\",\"Get Google Search Results for prior week\",\"Get Country views data for this week\",\"Get Country views data for prior week\"],lengths=[4,4,8,8,7,7],tables=[];for(let i=0;i{if(!Array.isArray(x.dimensionValues)||x.dimensionValues.length!==1||typeof x.dimensionValues[0]?.value!=='string'||!Array.isArray(x.metricValues)||x.metricValues.length!==lengths[i]||x.metricValues.some(v=>typeof v?.value!=='string'))throw Error('Invalid analytics row');return [x.dimensionValues[0].value,...x.metricValues.map(v=>v.value)]}))}const state={index:6,day:$('Prepare report dates').first().json.day,tables};return [{json:format({state,recipient:'YOUR_REPORT_EMAIL'})}];" }, "id": "email-a-google-analytics-comparison-report-8" }, { "id": "email-a-google-analytics-comparison-report-9", "name": "Email the Report", "type": "n8n-nodes-base.gmail", "position": [ 1040, 1480 ], "parameters": { "sendTo": "={{ $json.to }}", "subject": "={{ $json.subject }}", "message": "={{ $json.body }}", "options": { "appendAttribution": false } }, "typeVersion": 2.1 } ], "active": false, "pinData": {}, "settings": { "timezone": "UTC", "callerPolicy": "workflowsFromSameOwner", "executionOrder": "v1", "executionTimeout": -1, "saveManualExecutions": false }, "connections": { "When clicking ‘Test workflow’": { "main": [ [ { "node": "Prepare report dates", "type": "main", "index": 0 } ] ] }, "Prepare report dates": { "main": [ [ { "node": "Get Page Engagement Stats for this week", "type": "main", "index": 0 } ] ] }, "Get Page Engagement Stats for this week": { "main": [ [ { "node": "Get Page Engagement Stats for prior week", "type": "main", "index": 0 } ] ] }, "Get Page Engagement Stats for prior week": { "main": [ [ { "node": "Get Google Search Results for this week", "type": "main", "index": 0 } ] ] }, "Get Google Search Results for this week": { "main": [ [ { "node": "Get Google Search Results for prior week", "type": "main", "index": 0 } ] ] }, "Get Google Search Results for prior week": { "main": [ [ { "node": "Get Country views data for this week", "type": "main", "index": 0 } ] ] }, "Get Country views data for this week": { "main": [ [ { "node": "Get Country views data for prior week", "type": "main", "index": 0 } ] ] }, "Get Country views data for prior week": { "main": [ [ { "node": "Format the comparison", "type": "main", "index": 0 } ] ] }, "Format the comparison": { "main": [ [ { "node": "Email the Report", "type": "main", "index": 0 } ] ] } } }