{
  "$schema": "https://walkeros.io/schema/flow/v4.json",
  "version": 4,
  "variables": {
    "currency": "EUR",
    "flowVersion": "1.0.0",
    "ga4MeasurementId": "$env.GA4_MEASUREMENT_ID:G-DEMO123456",
    "apiUrl": "$env.API_URL:http://localhost:8080/collect",
    "ga4ItemsLoop": {
      "loop": [
        "nested",
        {
          "condition": "$code:(value, context) => value.entity === 'product'",
          "map": {
            "item_id": "data.id",
            "item_name": "data.name",
            "price": "data.price",
            "quantity": {
              "key": "data.quantity",
              "value": 1
            }
          }
        }
      ]
    },
    "metaContentsLoop": {
      "loop": [
        "nested",
        {
          "map": {
            "id": "data.id",
            "item_price": "data.price",
            "quantity": {
              "key": "data.quantity",
              "value": 1
            }
          }
        }
      ]
    }
  },
  "contract": {
    "default": {
      "description": "Web shop tracking contract",
      "schema": {
        "type": "object",
        "properties": {
          "globals": {
            "type": "object",
            "properties": {
              "environment": {
                "type": "string"
              },
              "version": {
                "type": "string"
              }
            }
          },
          "consent": {
            "type": "object",
            "required": ["functional"],
            "properties": {
              "functional": {
                "type": "boolean",
                "const": true
              }
            }
          }
        }
      },
      "events": {
        "product": {
          "*": {
            "description": "A product in the catalog",
            "properties": {
              "data": {
                "type": "object",
                "required": ["id", "name"],
                "properties": {
                  "id": {
                    "type": "string",
                    "description": "Product SKU"
                  },
                  "name": {
                    "type": "string",
                    "description": "Display name"
                  }
                }
              }
            }
          },
          "add": {
            "description": "Product added to cart",
            "properties": {
              "data": {
                "type": "object",
                "required": ["quantity"],
                "properties": {
                  "quantity": {
                    "type": "integer",
                    "minimum": 1
                  }
                }
              }
            }
          }
        },
        "order": {
          "complete": {
            "description": "Purchase completed",
            "properties": {
              "data": {
                "type": "object",
                "required": ["id", "total"],
                "properties": {
                  "id": {
                    "type": "string"
                  },
                  "total": {
                    "type": "number",
                    "minimum": 0
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "flows": {
    "web": {
      "sources": {
        "browser": {
          "package": "@walkeros/web-source-browser",
          "primary": true,
          "config": {
            "settings": {
              "prefix": "data-elb",
              "pageview": true,
              "session": {
                "consent": {
                  "marketing": true
                }
              },
              "elb": "elb",
              "elbLayer": true
            }
          }
        },
        "dataLayer": {
          "package": "@walkeros/web-source-datalayer",
          "next": "enricher",
          "config": {
            "require": ["session"],
            "settings": {
              "name": "dataLayer",
              "prefix": "gtag"
            },
            "mapping": {
              "gtag": {
                "add_to_cart": {
                  "name": "product add",
                  "data": {
                    "map": {
                      "id": "items.0.item_id",
                      "name": "items.0.item_name",
                      "price": "items.0.price",
                      "currency": "currency",
                      "quantity": {
                        "key": "items.0.quantity",
                        "value": 1
                      }
                    }
                  }
                },
                "purchase": {
                  "name": "order complete",
                  "data": {
                    "map": {
                      "id": "transaction_id",
                      "total": "value",
                      "currency": "currency",
                      "tax": "tax",
                      "shipping": "shipping"
                    }
                  }
                }
              }
            }
          }
        },
        "demo": {
          "package": "@walkeros/source-demo",
          "config": {
            "settings": {
              "events": [
                {
                  "name": "page view",
                  "data": {
                    "title": "Home",
                    "path": "/"
                  },
                  "delay": 0
                },
                {
                  "name": "product view",
                  "data": {
                    "id": "SKU-001",
                    "name": "Blue Shirt",
                    "price": 49.99,
                    "currency": "EUR",
                    "category": "Clothing"
                  },
                  "delay": 300
                },
                {
                  "name": "product add",
                  "data": {
                    "id": "SKU-001",
                    "name": "Blue Shirt",
                    "price": 49.99,
                    "currency": "EUR",
                    "quantity": 2
                  },
                  "delay": 600
                },
                {
                  "name": "test debug",
                  "data": {
                    "message": "This should be ignored"
                  },
                  "delay": 800
                },
                {
                  "name": "order complete",
                  "data": {
                    "id": "ORD-123",
                    "total": 149.97,
                    "currency": "EUR",
                    "tax": 12.5,
                    "shipping": 5.99
                  },
                  "user": {
                    "email": "customer@example.com",
                    "id": "U-456",
                    "device": "d3v1c3",
                    "session": "s3ss10n"
                  },
                  "nested": [
                    {
                      "entity": "product",
                      "data": {
                        "id": "SKU-001",
                        "name": "Blue Shirt",
                        "price": 49.99,
                        "quantity": 2
                      }
                    },
                    {
                      "entity": "product",
                      "data": {
                        "id": "SKU-002",
                        "name": "Red Cap",
                        "price": 24.99,
                        "quantity": 1
                      }
                    }
                  ],
                  "delay": 1000
                }
              ]
            }
          }
        }
      },
      "transformers": {
        "enricher": {
          "code": {
            "type": "enricher",
            "push": "$code:(event) => ({ ...event, context: { ...event.context, enrichedAt: Date.now() } })"
          },
          "config": {}
        }
      },
      "destinations": {
        "ga4": {
          "package": "@walkeros/web-destination-gtag",
          "config": {
            "require": ["consent", "user"],
            "consent": {
              "marketing": true
            },
            "settings": {
              "ga4": {
                "measurementId": "$var.ga4MeasurementId"
              }
            },
            "mapping": {
              "page": {
                "view": {
                  "name": "page_view",
                  "data": {
                    "map": {
                      "page_title": "data.title",
                      "page_location": "data.path"
                    }
                  }
                }
              },
              "product": {
                "view": {
                  "name": "view_item",
                  "data": {
                    "map": {
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "value": "data.price",
                      "items": {
                        "loop": [
                          "this",
                          {
                            "map": {
                              "item_id": [
                                {
                                  "key": "data.sku"
                                },
                                {
                                  "key": "data.id"
                                }
                              ],
                              "item_name": "data.name",
                              "item_category": "data.category",
                              "price": "data.price"
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                "add": {
                  "name": "add_to_cart",
                  "data": {
                    "map": {
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "value": "data.price",
                      "items": {
                        "loop": [
                          "this",
                          {
                            "map": {
                              "item_id": "data.id",
                              "item_name": "data.name",
                              "quantity": {
                                "key": "data.quantity",
                                "value": 1
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              },
              "order": {
                "complete": {
                  "name": "purchase",
                  "data": {
                    "map": {
                      "transaction_id": "data.id",
                      "value": {
                        "key": "data.total",
                        "fn": "$code:(v) => Math.round(v * 100)"
                      },
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "tax": "data.tax",
                      "shipping": "data.shipping",
                      "items": "$var.ga4ItemsLoop"
                    }
                  }
                }
              },
              "test": {
                "*": {
                  "ignore": true
                }
              }
            }
          },
          "examples": {
            "page-view": {
              "in": {
                "name": "page view",
                "data": {
                  "title": "Home",
                  "path": "/"
                },
                "entity": "page",
                "action": "view"
              },
              "mapping": {
                "name": "page_view",
                "data": {
                  "map": {
                    "page_title": "data.title",
                    "page_location": "data.path"
                  }
                }
              },
              "out": [
                "event",
                "page_view",
                {
                  "page_title": "Home",
                  "page_location": "/"
                }
              ]
            },
            "product-add": {
              "in": {
                "name": "product add",
                "data": {
                  "id": "SKU-001",
                  "name": "Blue Shirt",
                  "price": 49.99,
                  "currency": "EUR",
                  "quantity": 2
                },
                "entity": "product",
                "action": "add"
              },
              "mapping": {
                "name": "add_to_cart",
                "data": {
                  "map": {
                    "currency": {
                      "key": "data.currency",
                      "value": "EUR"
                    },
                    "value": "data.price",
                    "items": {
                      "loop": [
                        "this",
                        {
                          "map": {
                            "item_id": "data.id",
                            "item_name": "data.name",
                            "quantity": {
                              "key": "data.quantity",
                              "value": 1
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              },
              "out": [
                "event",
                "add_to_cart",
                {
                  "currency": "EUR",
                  "value": 49.99,
                  "items": [
                    {
                      "item_id": "SKU-001",
                      "item_name": "Blue Shirt",
                      "quantity": 2
                    }
                  ]
                }
              ]
            },
            "test-ignored": {
              "in": {
                "name": "test debug",
                "data": {
                  "message": "This should be ignored"
                },
                "entity": "test",
                "action": "debug"
              },
              "out": false
            }
          }
        },
        "api": {
          "package": "@walkeros/web-destination-api",
          "config": {
            "settings": {
              "url": "$var.apiUrl",
              "batch": 5,
              "transform": "$code:(data) => JSON.stringify(data)"
            },
            "data": {
              "map": {
                "sent_at": {
                  "fn": "$code:() => Date.now()"
                },
                "flow_version": "$var.flowVersion"
              }
            },
            "mapping": {
              "order": {
                "complete": {
                  "data": {
                    "map": {
                      "transaction_id": "data.id",
                      "value": "data.total",
                      "currency": "data.currency",
                      "user_email": {
                        "key": "user.email",
                        "consent": {
                          "marketing": true
                        }
                      },
                      "items": "$var.ga4ItemsLoop"
                    }
                  }
                }
              },
              "test": {
                "*": {
                  "ignore": true
                }
              }
            }
          }
        },
        "debug": {
          "code": {
            "type": "debug-logger",
            "push": "$code:(event, ctx) => { ctx.logger.info('[DEBUG]', event.name, event.data); }"
          },
          "config": {
            "consent": {}
          }
        }
      },
      "collector": {
        "consent": {
          "functional": true,
          "marketing": false
        },
        "user": {
          "id": "anonymous"
        },
        "globals": {
          "environment": "demo",
          "version": "$var.flowVersion"
        },
        "custom": {
          "campaign": "flow-demo"
        }
      },
      "config": {
        "platform": "web",
        "bundle": {
          "packages": {
            "@walkeros/collector": {},
            "@walkeros/web-source-browser": {},
            "@walkeros/web-source-datalayer": {},
            "@walkeros/source-demo": {},
            "@walkeros/web-destination-gtag": {},
            "@walkeros/web-destination-api": {}
          }
        }
      }
    },
    "server": {
      "variables": {
        "metaPixelId": "$env.META_PIXEL_ID:123456789012345",
        "metaAccessToken": "$env.META_ACCESS_TOKEN:demo_token"
      },
      "sources": {
        "http": {
          "package": "@walkeros/server-source-express",
          "primary": true,
          "next": ["filter"],
          "cache": {
            "rules": [
              {
                "match": {
                  "key": "ingest.method",
                  "operator": "eq",
                  "value": "GET"
                },
                "key": ["ingest.method", "ingest.path"],
                "ttl": 300,
                "update": {
                  "headers.X-Cache": {
                    "key": "cache.status"
                  }
                }
              }
            ]
          },
          "config": {
            "settings": {
              "path": "/collect",
              "port": 8080,
              "cors": true,
              "healthCheck": true
            },
            "ingest": {
              "map": {
                "method": { "key": "method" },
                "path": { "key": "path" },
                "context": {
                  "map": {
                    "ip": { "key": "ip" },
                    "userAgent": { "key": "headers.user-agent" },
                    "language": { "key": "headers.accept-language" },
                    "referer": { "key": "headers.referer" },
                    "anonymizedIp": {
                      "key": "ip",
                      "fn": "$code:(ip) => ip ? ip.replace(/\\.\\d+$/, '.0') : ''"
                    }
                  }
                }
              }
            }
          },
          "examples": {
            "post-event": {
              "in": {
                "method": "POST",
                "path": "/collect",
                "headers": {
                  "content-type": "application/json"
                },
                "body": {
                  "name": "page view",
                  "data": {
                    "title": "Home",
                    "url": "https://example.com"
                  }
                }
              },
              "out": {
                "name": "page view",
                "data": {
                  "title": "Home",
                  "url": "https://example.com"
                },
                "entity": "page",
                "action": "view"
              }
            }
          }
        }
      },
      "transformers": {
        "filter": {
          "code": {
            "type": "filter",
            "push": "$code:(event) => event.name.startsWith('internal_') || event.name.startsWith('debug_') ? false : event"
          },
          "next": "fingerprint",
          "config": {},
          "examples": {
            "passes-normal": {
              "in": {
                "name": "page view",
                "data": {
                  "title": "Home"
                },
                "entity": "page",
                "action": "view"
              },
              "out": {
                "name": "page view",
                "data": {
                  "title": "Home"
                },
                "entity": "page",
                "action": "view"
              }
            },
            "filters-internal": {
              "in": {
                "name": "internal_heartbeat",
                "data": {},
                "entity": "internal",
                "action": "heartbeat"
              },
              "out": false
            }
          }
        },
        "fingerprint": {
          "package": "@walkeros/server-transformer-fingerprint",
          "config": {
            "settings": {
              "fields": [
                "context.ip",
                "context.userAgent",
                "context.language",
                {
                  "fn": "$code:() => new Date().toISOString().slice(0, 10)"
                }
              ],
              "output": "user.hash",
              "length": 16
            }
          }
        }
      },
      "destinations": {
        "meta": {
          "package": "@walkeros/server-destination-meta",
          "before": "fingerprint",
          "config": {
            "settings": {
              "pixelId": "$var.metaPixelId",
              "accessToken": "$var.metaAccessToken",
              "user_data": {
                "external_id": {
                  "set": ["user.device", "user.session"]
                }
              }
            },
            "policy": {
              "user_data.em": {
                "key": "user.email",
                "consent": {
                  "marketing": true
                }
              },
              "user_data.external_id": "user.id",
              "custom_data.server_processed": {
                "value": true
              },
              "custom_data.request_meta": {
                "map": {
                  "ip": "context.ip",
                  "ua": "context.userAgent"
                }
              }
            },
            "mapping": {
              "page": {
                "view": {
                  "name": "PageView",
                  "data": "data"
                }
              },
              "product": {
                "view": {
                  "name": "ViewContent",
                  "data": {
                    "map": {
                      "value": "data.price",
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "content_type": {
                        "value": "product"
                      },
                      "content_ids": {
                        "set": ["data.id"]
                      }
                    }
                  }
                },
                "add": {
                  "name": "AddToCart",
                  "data": {
                    "map": {
                      "value": "data.price",
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "content_type": {
                        "value": "product"
                      },
                      "contents": {
                        "loop": [
                          "this",
                          {
                            "map": {
                              "id": "data.id",
                              "quantity": {
                                "key": "data.quantity",
                                "value": 1
                              }
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              },
              "order": {
                "complete": {
                  "name": "Purchase",
                  "data": {
                    "map": {
                      "value": "data.total",
                      "currency": {
                        "key": "data.currency",
                        "value": "$var.currency"
                      },
                      "order_id": "data.id",
                      "contents": "$var.metaContentsLoop"
                    }
                  }
                }
              },
              "test": {
                "*": {
                  "ignore": true
                }
              },
              "*": {
                "click": {
                  "name": "CustomEvent",
                  "data": "data"
                }
              }
            }
          },
          "examples": {
            "purchase": {
              "in": {
                "name": "order complete",
                "data": {
                  "id": "ORD-123",
                  "total": 149.97,
                  "currency": "EUR"
                },
                "entity": "order",
                "action": "complete"
              },
              "mapping": {
                "name": "Purchase",
                "data": {
                  "map": {
                    "value": "data.total",
                    "currency": {
                      "key": "data.currency",
                      "value": "EUR"
                    },
                    "order_id": "data.id"
                  }
                }
              },
              "out": {
                "data": [
                  {
                    "event_name": "Purchase",
                    "custom_data": {
                      "value": 149.97,
                      "currency": "EUR",
                      "order_id": "ORD-123"
                    }
                  }
                ]
              }
            }
          }
        },
        "demo": {
          "package": "@walkeros/destination-demo",
          "config": {
            "settings": {
              "name": "Server Events",
              "values": ["name", "data", "nested", "user", "consent", "context"]
            }
          }
        }
      },
      "collector": {
        "consent": {
          "functional": true
        },
        "globals": {
          "environment": "demo",
          "version": "$var.flowVersion",
          "platform": "server",
          "startedAt": "$code:Date.now()"
        }
      },
      "config": {
        "platform": "server",
        "bundle": {
          "packages": {
            "@walkeros/core": {
              "path": "../../core"
            },
            "@walkeros/collector": {
              "path": "../../collector"
            },
            "@walkeros/server-source-express": {},
            "@walkeros/server-destination-meta": {},
            "@walkeros/destination-demo": {},
            "@walkeros/server-transformer-fingerprint": {}
          }
        }
      }
    }
  }
}
