{
	"name": "Shopify orders a few days after each post",
	"nodes": [
		{
			"id": "55d2db2e-8689-448c-b733-86ff8b32765e",
			"name": "Read me first",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-1080,
				60
			],
			"parameters": {
				"content": "## Shopify orders a few days after each post\nA few days after each post you get a Telegram message with the Shopify orders that came in through its link. Each result is saved, and every Monday you get the best posts of the week.\n\n### You need\n- The sprkly node: Settings, Community nodes, install `n8n-nodes-sprkly` (self-hosted n8n)\n- A sprkly API key: https://sprkly.app/docs/api?utm_source=n8n&utm_medium=workflow&utm_campaign=shopify-post-follow-up\n- A Shopify login that can read orders\n- A Telegram bot and your chat ID\n- Links in your posts that end with ?utm_campaign=sprkly- and the post External ID\n- A Google Sheets login and a Post Follow-ups tab with: Post ID, Platform, Went Out, Post Link, Orders, Revenue, Currency, Link Tag, Checked At\n\n### Set it up\n1. Add your sprkly key to **Get posts that went out** and your Shopify login to **Get Shopify orders**.\n2. Add your bot to the Telegram steps. In **Settings**, add your chat ID and time zone, and set daysAfter to a whole number from 3 to 7.\n3. Pick your sheet in **Save the follow-up** and **Read the follow-ups**.\n4. Failure alert: put your chat ID in **Write the failure alert** and your bot in **Send the failure alert**. Then in Workflow settings, set Error workflow to this workflow.\n\n### Test it\n- Set daysAfter to 1 on the day after a post went out. Run the workflow once. You get one message per post and one row each.\n- Click Test workflow with **Every Monday** picked to see the roll-up. Set daysAfter back after.",
				"height": 962,
				"width": 560,
				"color": 4
			}
		},
		{
			"id": "4a3de218-4e4f-49c0-b56e-54077f499fa3",
			"name": "Note: find the posts",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-440,
				60
			],
			"parameters": {
				"content": "## Find the posts\nGets the posts that went out, then keeps the ones that went out on the day `daysAfter` days ago, in your time zone.",
				"height": 600,
				"width": 1140,
				"color": 7
			}
		},
		{
			"id": "9c6e2f2d-f887-41dc-8b98-fc3e0b2379b6",
			"name": "Note: count the orders",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				720,
				60
			],
			"parameters": {
				"content": "## Count the orders\nAn order counts when the page the buyer landed on has this post's tag. Cancelled orders are left out. A post sent to several accounts shares one tag, so each of its messages shows the same orders.",
				"height": 600,
				"width": 1100,
				"color": 7
			}
		},
		{
			"id": "624b3c9d-5718-4078-bc01-1493653e649b",
			"name": "Note: send the follow-up",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				1840,
				60
			],
			"parameters": {
				"content": "## Send the follow-up\nOne message per post. When no order came in through the link, the message says so.",
				"height": 600,
				"width": 480,
				"color": 7
			}
		},
		{
			"id": "54807ad4-1b3d-4d45-9399-da1b08802763",
			"name": "Every morning",
			"type": "n8n-nodes-base.scheduleTrigger",
			"typeVersion": 1.3,
			"position": [
				-400,
				320
			],
			"parameters": {
				"rule": {
					"interval": [
						{
							"field": "days",
							"triggerAtHour": 9
						}
					]
				}
			}
		},
		{
			"id": "ae4c1e53-37c6-41c3-a55e-b2bd81433458",
			"name": "Settings",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-180,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "daysAfter",
							"type": "number",
							"value": 3
						},
						{
							"id": "f2",
							"name": "utmCampaignPrefix",
							"type": "string",
							"value": "sprkly-"
						},
						{
							"id": "f3",
							"name": "timeZone",
							"type": "string",
							"value": "UTC"
						},
						{
							"id": "f4",
							"name": "telegramChatId",
							"type": "string",
							"value": "YOUR_CHAT_ID"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "01e3661e-50e0-4526-8f27-1ce222516971",
			"name": "Get posts that went out",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				280,
				320
			],
			"parameters": {
				"resource": "post",
				"operation": "getAll",
				"returnAll": true,
				"filters": {
					"status": "posted"
				},
				"simplify": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "fac9a519-68cf-429f-a5f5-d7ff79d2828a",
			"name": "Went out on the day to check",
			"type": "n8n-nodes-base.filter",
			"typeVersion": 2.2,
			"position": [
				500,
				320
			],
			"parameters": {
				"conditions": {
					"options": {
						"caseSensitive": true,
						"leftValue": "",
						"typeValidation": "loose",
						"version": 2
					},
					"conditions": [
						{
							"id": "went-out-that-day",
							"leftValue": "={{ !!$json.id && !$json.deleted && !!$json.publishedAt && DateTime.fromISO($json.publishedAt).setZone($('Settings').first().json.timeZone || 'UTC').startOf('day').toMillis() === $now.setZone($('Settings').first().json.timeZone || 'UTC').startOf('day').minus({ days: Math.max(1, Math.round(Number($('Settings').first().json.daysAfter) || 3)) }).toMillis() }}",
							"rightValue": "",
							"operator": {
								"type": "boolean",
								"operation": "true",
								"singleValue": true
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "2a60440f-71dd-40af-b6e5-ce763d34bb00",
			"name": "Get Shopify orders",
			"type": "n8n-nodes-base.shopify",
			"typeVersion": 1,
			"position": [
				760,
				480
			],
			"parameters": {
				"authentication": "accessToken",
				"resource": "order",
				"operation": "getAll",
				"returnAll": true,
				"options": {
					"createdAtMin": "={{ $now.setZone($('Settings').first().json.timeZone || 'UTC').startOf('day').minus({ days: Math.max(1, Math.round(Number($('Settings').first().json.daysAfter) || 3)) }).toISO() }}",
					"status": "any",
					"fields": "id,created_at,cancelled_at,landing_site,total_price,currency"
				}
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "51f5db88-3cba-44e4-a18d-5e6864693b8d",
			"name": "Wait for both",
			"type": "n8n-nodes-base.merge",
			"typeVersion": 3.2,
			"position": [
				980,
				320
			],
			"parameters": {
				"mode": "chooseBranch",
				"useDataOfInput": 1
			}
		},
		{
			"id": "bf72f054-d1c1-4282-8b6a-ca9f80819591",
			"name": "Pick the details",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				1200,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "tag",
							"type": "string",
							"value": "={{ $('Settings').first().json.utmCampaignPrefix + (String($json.externalId || '').endsWith(':' + ($json.platforms || [])[0] + ':' + ($json.profileIds || [])[0]) && String($json.externalId).length > (':' + ($json.platforms || [])[0] + ':' + ($json.profileIds || [])[0]).length ? String($json.externalId).slice(0, -(':' + ($json.platforms || [])[0] + ':' + ($json.profileIds || [])[0]).length) : String($json.externalId || '') || $json.id) }}"
						},
						{
							"id": "f2",
							"name": "platform",
							"type": "string",
							"value": "={{ String(($json.platforms || [])[0] || 'unknown') }}"
						},
						{
							"id": "f3",
							"name": "wentOut",
							"type": "string",
							"value": "={{ DateTime.fromISO($json.publishedAt).setZone($('Settings').first().json.timeZone || 'UTC').toFormat('d LLL yyyy, HH:mm') + ' ' + ($('Settings').first().json.timeZone || 'UTC') }}"
						},
						{
							"id": "f4",
							"name": "caption",
							"type": "string",
							"value": "={{ String($json.caption || '').length > 80 ? String($json.caption).slice(0, 77) + '...' : String($json.caption || '(no caption)') }}"
						},
						{
							"id": "f5",
							"name": "postLink",
							"type": "string",
							"value": "={{ $json.permalink || 'https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=shopify-post-follow-up&post=' + encodeURIComponent($json.id) }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "e4818f68-25e7-42e2-80a2-dd4fe79f0c45",
			"name": "Find this post's orders",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				1420,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "orders",
							"type": "array",
							"value": "={{ $('Get Shopify orders').all().map(o => o.json).filter(o => o.id && !o.cancelled_at && DateTime.fromISO(String(o.created_at)).toMillis() >= DateTime.fromISO($json.publishedAt).toMillis() && ('&' + String(o.landing_site || '').split('#')[0].split('?').slice(1).join('&') + '&').toLowerCase().includes('&utm_campaign=' + String($json.tag).toLowerCase() + '&')) }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "76569fa2-0f67-4a9d-ac3a-eacd96dcea27",
			"name": "Write the message",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				1640,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "message",
							"type": "string",
							"value": "={{ 'A follow-up on your post from ' + Math.max(1, Math.round(Number($('Settings').first().json.daysAfter) || 3)) + ' days ago.\\n\\nPost: ' + $json.caption + '\\nWhere: ' + $json.platform + '\\nWent out: ' + $json.wentOut + '\\nSee it: ' + $json.postLink + '\\n\\n' + (($json.orders || []).length === 0 ? \"No orders came in through this post's link yet.\" : \"Orders that came in through this post's link: \" + $json.orders.length + '\\nTotal: ' + $json.orders.reduce((sum, o) => sum + (Number(o.total_price) || 0), 0).toFixed(2) + ' ' + String($json.orders[0].currency || '')) + '\\n\\nLink tag: utm_campaign=' + $json.tag }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "4a3de218-4e4f-49c0-b56e-54077f499fa4",
			"name": "Send to Telegram",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				1880,
				320
			],
			"parameters": {
				"chatId": "={{ $('Settings').first().json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		},
		{
			"id": "900bb082-a2dc-49d6-809f-3be3177e0b41",
			"name": "When this workflow fails",
			"type": "n8n-nodes-base.errorTrigger",
			"typeVersion": 1,
			"position": [
				-1040,
				1102
			],
			"parameters": {}
		},
		{
			"id": "17c0a38b-cb7c-4260-b750-ef34b89d2906",
			"name": "Write the failure alert",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-820,
				1102
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "telegramChatId",
							"type": "string",
							"value": "YOUR_TELEGRAM_CHAT_ID"
						},
						{
							"id": "f2",
							"name": "message",
							"type": "string",
							"value": "={{ 'A workflow stopped.\\n\\nWorkflow: ' + (($json.workflow || {}).name || 'unknown') + '\\nFailed step: ' + (($json.execution || {}).lastNodeExecuted || 'unknown') + '\\nWhy: ' + String((($json.execution || {}).error || {}).message || 'n8n did not give a reason.').slice(0, 300) + (($json.execution || {}).url ? '\\nOpen the run: ' + $json.execution.url : '') }}"
						}
					]
				},
				"includeOtherFields": false,
				"options": {}
			}
		},
		{
			"id": "d458b175-e984-442f-a9d3-74e12156f626",
			"name": "Send the failure alert",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				-600,
				1102
			],
			"parameters": {
				"chatId": "={{ $json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		},
		{
			"id": "1e9b0f08-b6fb-4eb6-a1f4-87c56793d904",
			"name": "Every Monday",
			"type": "n8n-nodes-base.scheduleTrigger",
			"typeVersion": 1.3,
			"position": [
				-400,
				500
			],
			"parameters": {
				"rule": {
					"interval": [
						{
							"field": "weeks",
							"triggerAtDay": [
								1
							],
							"triggerAtHour": 9
						}
					]
				}
			}
		},
		{
			"id": "98920ccc-72a3-477a-80f2-2e8b98eedafc",
			"name": "Monday roll-up?",
			"type": "n8n-nodes-base.if",
			"typeVersion": 2.2,
			"position": [
				40,
				320
			],
			"parameters": {
				"conditions": {
					"options": {
						"caseSensitive": true,
						"leftValue": "",
						"typeValidation": "loose",
						"version": 2
					},
					"conditions": [
						{
							"id": "c1",
							"leftValue": "={{ $('Every Monday').isExecuted }}",
							"rightValue": "",
							"operator": {
								"type": "boolean",
								"operation": "true",
								"singleValue": true
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "c12bf11d-d93e-47e3-82c0-9ce0bc4c8e3d",
			"name": "Save the follow-up",
			"type": "n8n-nodes-base.googleSheets",
			"typeVersion": 4.7,
			"position": [
				1640,
				520
			],
			"parameters": {
				"operation": "appendOrUpdate",
				"documentId": {
					"__rl": true,
					"value": "",
					"mode": "url"
				},
				"sheetName": {
					"__rl": true,
					"value": "Post Follow-ups",
					"mode": "name"
				},
				"columns": {
					"mappingMode": "defineBelow",
					"value": {
						"Post ID": "={{ $json.id }}",
						"Platform": "={{ $json.platform }}",
						"Went Out": "={{ $json.wentOut }}",
						"Post Link": "={{ $json.postLink }}",
						"Orders": "={{ ($json.orders || []).length }}",
						"Revenue": "={{ ($json.orders || []).reduce((sum, o) => sum + (Number(o.total_price) || 0), 0).toFixed(2) }}",
						"Currency": "={{ String((($json.orders || [])[0] || {}).currency || '') }}",
						"Link Tag": "={{ $json.tag }}",
						"Checked At": "={{ $now.toISO() }}"
					},
					"matchingColumns": [
						"Post ID"
					],
					"schema": [
						{
							"id": "Post ID",
							"displayName": "Post ID",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Platform",
							"displayName": "Platform",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Went Out",
							"displayName": "Went Out",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Post Link",
							"displayName": "Post Link",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Orders",
							"displayName": "Orders",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Revenue",
							"displayName": "Revenue",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Currency",
							"displayName": "Currency",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Link Tag",
							"displayName": "Link Tag",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Checked At",
							"displayName": "Checked At",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						}
					]
				},
				"options": {}
			}
		},
		{
			"id": "74894ad0-c62f-40b7-b841-a1a111b373c0",
			"name": "Note: weekly roll-up",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				240,
				700
			],
			"parameters": {
				"content": "## Monday roll-up\nEvery Monday: the follow-ups saved in the last 7 days, with the posts that brought the most orders.",
				"height": 320,
				"width": 940,
				"color": 7
			}
		},
		{
			"id": "181af886-861d-4e59-acd6-ac3fafe4370b",
			"name": "Read the follow-ups",
			"type": "n8n-nodes-base.googleSheets",
			"typeVersion": 4.7,
			"position": [
				280,
				820
			],
			"parameters": {
				"operation": "read",
				"documentId": {
					"__rl": true,
					"value": "",
					"mode": "url"
				},
				"sheetName": {
					"__rl": true,
					"value": "Post Follow-ups",
					"mode": "name"
				},
				"options": {}
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "38d66993-d265-4fbf-bab2-d316f76c8ece",
			"name": "Write the roll-up",
			"type": "n8n-nodes-base.code",
			"typeVersion": 2,
			"position": [
				500,
				820
			],
			"parameters": {
				"mode": "runOnceForAllItems",
				"jsCode": "const since = $now.minus({ days: 7 });\nconst rows = $input.all().map(i => i.json).filter(r => r['Post ID'] && DateTime.fromISO(String(r['Checked At'] || '')) >= since);\nif (rows.length === 0) return [{ json: { message: 'No post follow-ups in the last 7 days.' } }];\nconst orders = rows.reduce((s, r) => s + (Number(r.Orders) || 0), 0);\nconst revenue = rows.reduce((s, r) => s + (Number(r.Revenue) || 0), 0);\nconst currency = (rows.find(r => r.Currency) || {}).Currency || '';\nconst top = rows.filter(r => Number(r.Orders) > 0).sort((a, b) => (Number(b.Revenue) || 0) - (Number(a.Revenue) || 0)).slice(0, 5);\nconst lines = top.map(r => '- ' + r.Platform + ', ' + r['Went Out'] + ': ' + r.Orders + ' orders, ' + r.Revenue + ' ' + (r.Currency || '') + '\\n  ' + r['Post Link']);\nreturn [{ json: { message: 'Last 7 days of post follow-ups: ' + rows.length + ' posts\\nOrders through their links: ' + orders + '\\nTotal: ' + revenue.toFixed(2) + ' ' + currency + (lines.length ? '\\n\\nBest posts:\\n' + lines.join('\\n') : '\\n\\nNo orders came in through these links.') } }];"
			}
		},
		{
			"id": "f20f556b-04ad-4a6e-93d5-557743a6ff84",
			"name": "Send the roll-up",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				720,
				820
			],
			"parameters": {
				"chatId": "={{ $('Settings').first().json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		}
	],
	"connections": {
		"Every morning": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Settings": {
			"main": [
				[
					{
						"node": "Monday roll-up?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get posts that went out": {
			"main": [
				[
					{
						"node": "Went out on the day to check",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Went out on the day to check": {
			"main": [
				[
					{
						"node": "Wait for both",
						"type": "main",
						"index": 0
					},
					{
						"node": "Get Shopify orders",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get Shopify orders": {
			"main": [
				[
					{
						"node": "Wait for both",
						"type": "main",
						"index": 1
					}
				]
			]
		},
		"Wait for both": {
			"main": [
				[
					{
						"node": "Pick the details",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Pick the details": {
			"main": [
				[
					{
						"node": "Find this post's orders",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Find this post's orders": {
			"main": [
				[
					{
						"node": "Write the message",
						"type": "main",
						"index": 0
					},
					{
						"node": "Save the follow-up",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Write the message": {
			"main": [
				[
					{
						"node": "Send to Telegram",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"When this workflow fails": {
			"main": [
				[
					{
						"node": "Write the failure alert",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Write the failure alert": {
			"main": [
				[
					{
						"node": "Send the failure alert",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Every Monday": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Monday roll-up?": {
			"main": [
				[
					{
						"node": "Read the follow-ups",
						"type": "main",
						"index": 0
					}
				],
				[
					{
						"node": "Get posts that went out",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Read the follow-ups": {
			"main": [
				[
					{
						"node": "Write the roll-up",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Write the roll-up": {
			"main": [
				[
					{
						"node": "Send the roll-up",
						"type": "main",
						"index": 0
					}
				]
			]
		}
	},
	"pinData": {},
	"active": false,
	"settings": {
		"executionOrder": "v1"
	}
}
