|
| 1 | +# Dashboard Layout Example |
| 2 | +# |
| 3 | +# This example demonstrates the dashboard layout resource in PostHog: |
| 4 | +# 1. Create a dashboard |
| 5 | +# 2. Create insights to display on the dashboard |
| 6 | +# 3. Arrange tiles (insights + text) with precise grid positions |
| 7 | +# 4. Optionally: add color to highlight specific tiles |
| 8 | +# 5. Optionally: update a text tile body in place |
| 9 | +# 6. Optionally: add a new tile |
| 10 | +# 7. Optionally: remove a tile and reposition |
| 11 | + |
| 12 | +# ============================================================================= |
| 13 | +# Step 1: Create the dashboard |
| 14 | +# ============================================================================= |
| 15 | + |
| 16 | +resource "posthog_dashboard" "demo" { |
| 17 | + name = "Production Metrics" |
| 18 | + description = "Key metrics managed by Terraform" |
| 19 | +} |
| 20 | + |
| 21 | +# ============================================================================= |
| 22 | +# Step 2: Create insights |
| 23 | +# ============================================================================= |
| 24 | + |
| 25 | +resource "posthog_insight" "pageviews" { |
| 26 | + name = "Pageview Trends" |
| 27 | + query_json = jsonencode({ |
| 28 | + kind = "InsightVizNode" |
| 29 | + source = { |
| 30 | + kind = "TrendsQuery" |
| 31 | + series = [{ |
| 32 | + kind = "EventsNode" |
| 33 | + event = "$pageview" |
| 34 | + math = "total" |
| 35 | + }] |
| 36 | + } |
| 37 | + }) |
| 38 | + dashboard_ids = [posthog_dashboard.demo.id] |
| 39 | + depends_on = [posthog_dashboard.demo] |
| 40 | +} |
| 41 | + |
| 42 | +resource "posthog_insight" "sessions" { |
| 43 | + name = "Session Trends" |
| 44 | + query_json = jsonencode({ |
| 45 | + kind = "InsightVizNode" |
| 46 | + source = { |
| 47 | + kind = "TrendsQuery" |
| 48 | + series = [{ |
| 49 | + kind = "EventsNode" |
| 50 | + event = "$session_start" |
| 51 | + math = "total" |
| 52 | + }] |
| 53 | + } |
| 54 | + }) |
| 55 | + dashboard_ids = [posthog_dashboard.demo.id] |
| 56 | + depends_on = [posthog_dashboard.demo] |
| 57 | +} |
| 58 | + |
| 59 | +# ============================================================================= |
| 60 | +# Step 3: Arrange tiles on the dashboard |
| 61 | +# |
| 62 | +# The layout is authoritative: any tiles not listed here will have their |
| 63 | +# layouts cleared (insight tiles) or be soft-deleted (text tiles). |
| 64 | +# |
| 65 | +# Grid: 12 columns wide. Each tile needs x, y, w, h in at least the "sm" |
| 66 | +# breakpoint. |
| 67 | +# ============================================================================= |
| 68 | + |
| 69 | +resource "posthog_dashboard_layout" "demo" { |
| 70 | + dashboard_id = posthog_dashboard.demo.id |
| 71 | + |
| 72 | + tiles = [ |
| 73 | + { |
| 74 | + text_body = "## Production Metrics" |
| 75 | + layouts_json = jsonencode({ sm = { x = 0, y = 0, w = 12, h = 1 } }) |
| 76 | + }, |
| 77 | + { |
| 78 | + insight_id = posthog_insight.pageviews.id |
| 79 | + layouts_json = jsonencode({ sm = { x = 0, y = 1, w = 6, h = 5 } }) |
| 80 | + }, |
| 81 | + { |
| 82 | + insight_id = posthog_insight.sessions.id |
| 83 | + layouts_json = jsonencode({ sm = { x = 6, y = 1, w = 6, h = 5 } }) |
| 84 | + }, |
| 85 | + ] |
| 86 | + |
| 87 | + depends_on = [posthog_insight.pageviews, posthog_insight.sessions] |
| 88 | +} |
| 89 | + |
| 90 | +# ============================================================================= |
| 91 | +# Step 4 (Optional): Add color to highlight a tile |
| 92 | +# ============================================================================= |
| 93 | + |
| 94 | +# Uncomment to use: |
| 95 | + |
| 96 | +# resource "posthog_dashboard_layout" "demo" { |
| 97 | +# dashboard_id = posthog_dashboard.demo.id |
| 98 | +# |
| 99 | +# tiles = [ |
| 100 | +# { |
| 101 | +# text_body = "## Production Metrics" |
| 102 | +# layouts_json = jsonencode({ sm = { x = 0, y = 0, w = 12, h = 1 } }) |
| 103 | +# }, |
| 104 | +# { |
| 105 | +# insight_id = posthog_insight.pageviews.id |
| 106 | +# color = "blue" |
| 107 | +# layouts_json = jsonencode({ sm = { x = 0, y = 1, w = 6, h = 5 } }) |
| 108 | +# }, |
| 109 | +# { |
| 110 | +# insight_id = posthog_insight.sessions.id |
| 111 | +# layouts_json = jsonencode({ sm = { x = 6, y = 1, w = 6, h = 5 } }) |
| 112 | +# }, |
| 113 | +# ] |
| 114 | +# |
| 115 | +# depends_on = [posthog_insight.pageviews, posthog_insight.sessions] |
| 116 | +# } |
| 117 | + |
| 118 | +# ============================================================================= |
| 119 | +# Step 5 (Optional): Update text tile body in place |
| 120 | +# |
| 121 | +# Changing a text tile's body updates the existing tile (matched by position). |
| 122 | +# The tile_id stays stable — no destroy/recreate. |
| 123 | +# ============================================================================= |
| 124 | + |
| 125 | +# Uncomment to use (comment out Steps 3-4 first): |
| 126 | + |
| 127 | +# resource "posthog_dashboard_layout" "demo" { |
| 128 | +# dashboard_id = posthog_dashboard.demo.id |
| 129 | +# |
| 130 | +# tiles = [ |
| 131 | +# { |
| 132 | +# text_body = "## Updated Production Metrics" |
| 133 | +# layouts_json = jsonencode({ sm = { x = 0, y = 0, w = 12, h = 1 } }) |
| 134 | +# }, |
| 135 | +# { |
| 136 | +# insight_id = posthog_insight.pageviews.id |
| 137 | +# layouts_json = jsonencode({ sm = { x = 0, y = 1, w = 6, h = 5 } }) |
| 138 | +# }, |
| 139 | +# { |
| 140 | +# insight_id = posthog_insight.sessions.id |
| 141 | +# layouts_json = jsonencode({ sm = { x = 6, y = 1, w = 6, h = 5 } }) |
| 142 | +# }, |
| 143 | +# ] |
| 144 | +# |
| 145 | +# depends_on = [posthog_insight.pageviews, posthog_insight.sessions] |
| 146 | +# } |
| 147 | + |
| 148 | +# ============================================================================= |
| 149 | +# Step 6 (Optional): Add a footer text tile |
| 150 | +# |
| 151 | +# Adding a tile increases the count. Existing tiles keep their tile_ids. |
| 152 | +# ============================================================================= |
| 153 | + |
| 154 | +# Uncomment to use (comment out Steps 3-5 first): |
| 155 | + |
| 156 | +# resource "posthog_dashboard_layout" "demo" { |
| 157 | +# dashboard_id = posthog_dashboard.demo.id |
| 158 | +# |
| 159 | +# tiles = [ |
| 160 | +# { |
| 161 | +# text_body = "## Updated Production Metrics" |
| 162 | +# layouts_json = jsonencode({ sm = { x = 0, y = 0, w = 12, h = 1 } }) |
| 163 | +# }, |
| 164 | +# { |
| 165 | +# insight_id = posthog_insight.pageviews.id |
| 166 | +# layouts_json = jsonencode({ sm = { x = 0, y = 1, w = 6, h = 5 } }) |
| 167 | +# }, |
| 168 | +# { |
| 169 | +# insight_id = posthog_insight.sessions.id |
| 170 | +# layouts_json = jsonencode({ sm = { x = 6, y = 1, w = 6, h = 5 } }) |
| 171 | +# }, |
| 172 | +# { |
| 173 | +# text_body = "_Managed by Terraform_" |
| 174 | +# layouts_json = jsonencode({ sm = { x = 0, y = 6, w = 12, h = 1 } }) |
| 175 | +# }, |
| 176 | +# ] |
| 177 | +# |
| 178 | +# depends_on = [posthog_insight.pageviews, posthog_insight.sessions] |
| 179 | +# } |
| 180 | + |
| 181 | +# ============================================================================= |
| 182 | +# Step 7 (Optional): Remove an insight and reposition |
| 183 | +# |
| 184 | +# Removing a tile from the list clears its layout (insight tiles) or |
| 185 | +# soft-deletes it (text tiles). Remaining tiles can be repositioned freely. |
| 186 | +# ============================================================================= |
| 187 | + |
| 188 | +# Uncomment to use (comment out Steps 3-6 first): |
| 189 | + |
| 190 | +# resource "posthog_dashboard_layout" "demo" { |
| 191 | +# dashboard_id = posthog_dashboard.demo.id |
| 192 | +# |
| 193 | +# tiles = [ |
| 194 | +# { |
| 195 | +# text_body = "## Updated Production Metrics" |
| 196 | +# layouts_json = jsonencode({ sm = { x = 0, y = 0, w = 12, h = 1 } }) |
| 197 | +# }, |
| 198 | +# { |
| 199 | +# insight_id = posthog_insight.pageviews.id |
| 200 | +# layouts_json = jsonencode({ sm = { x = 0, y = 1, w = 12, h = 5 } }) |
| 201 | +# }, |
| 202 | +# ] |
| 203 | +# |
| 204 | +# depends_on = [posthog_insight.pageviews] |
| 205 | +# } |
| 206 | + |
| 207 | +# ============================================================================= |
| 208 | +# Outputs |
| 209 | +# ============================================================================= |
| 210 | + |
| 211 | +output "dashboard" { |
| 212 | + description = "Created dashboard details" |
| 213 | + value = { |
| 214 | + id = posthog_dashboard.demo.id |
| 215 | + name = posthog_dashboard.demo.name |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +output "layout" { |
| 220 | + description = "Dashboard layout ID" |
| 221 | + value = { |
| 222 | + id = posthog_dashboard_layout.demo.id |
| 223 | + dashboard_id = posthog_dashboard_layout.demo.dashboard_id |
| 224 | + tile_count = length(posthog_dashboard_layout.demo.tiles) |
| 225 | + } |
| 226 | +} |
0 commit comments