iOS Codegen Setup
A guide to integrating EventPanel CLI into your iOS project for type-safe analytics event code generation.
Demo Project: View iOS Demo on GitHub โ
Explore how analytics events are used in our iOS demo project with generated, type-safe Swift code.
Overviewโ
EventPanel CLI generates type-safe Swift analytics events from YAML configuration files, ensuring compile-time safety and reducing runtime errors in your iOS application.
Featuresโ
- ๐ฏ Type-Safe Analytics: Generated analytics events with compile-time safety
- ๐ YAML Configuration: Event definitions managed through
EventPanel.yaml - ๐ Code Generation: Automated Swift code generation from event schemas
- ๐ก๏ธ Type Safety: Compile-time validation of event parameters
Installationโ
Prerequisitesโ
- Xcode 14.0 or later
- Swift 5.7 or later
- macOS 12.0 or later
Install EventPanel CLIโ
Option 1: Homebrew (Recommended)โ
brew tap eventpanel/eventpanel
brew install eventpanel
Option 2: Manual Installationโ
Download the latest release from the EventPanel CLI repository and add it to your PATH.
Verify Installationโ
eventpanel --version
Project Initializationโ
Step 1: Initialize EventPanel Configurationโ
Run the initialization command in your project root directory:
eventpanel init --output DemoApp/Analytics/GeneratedAnalyticsEvents.swift
This command will:
- Create
EventPanel.yamlconfiguration file - Set up the default Swift configuration
- Configure the output path for generated events
Note: Replace the output path with your actual project structure. For example:
DemoApp/Analytics/GeneratedAnalyticsEvents.swiftYourApp/Sources/Analytics/GeneratedAnalyticsEvents.swiftApp/Analytics/GeneratedAnalyticsEvents.swift
The generated EventPanel.yaml will look like:
source: iOS
plugin:
swiftgen:
eventTypeName: AnalyticsEvent
outputFilePath: DemoApp/Analytics/GeneratedAnalyticsEvents.swift
shouldGenerateType: true
namespace: AnalyticsEvents
documentation: true
events: []
You can then add your events to the events array or configure them through the EventPanel web interface.
Step 2: Set API Tokenโ
After initialization, you need to authenticate with EventPanel by setting your API token:
-
Create an API Token:
- Go to your EventPanel workspace settings
- Navigate to the API tokens section
- Create a new token
-
Set the Token:
eventpanel set-token YOUR_API_TOKEN
The token will be securely stored and used for all authenticated requests to EventPanel.
Note: Replace YOUR_API_TOKEN with the actual token you created in the workspace settings.
Step 3: Add Events to Configurationโ
After initialization, you can add events to your EventPanel.yaml using the eventpanel add command. There are several ways to add events:
Add a Single Event by IDโ
# Add latest version of an event
eventpanel add DWnQMGoYrvUyaTGpbmvr9
# Add specific version of an event
eventpanel add DWnQMGoYrvUyaTGpbmvr9 --version 2
Add an Event by Nameโ
# Add latest version of an event by name
eventpanel add --name "User Login"
Add All Eventsโ
# Add all available events for the configured source
eventpanel add --all
Add Events by Categoryโ
# Add all events from a category by ID
eventpanel add --category-id abc123
# Add all events from a category by name
eventpanel add --category-name "Login Screen"
Manual Configurationโ
You can also manually edit EventPanel.yaml to add events:
events:
- id: YOUR_EVENT_ID_1
- id: YOUR_EVENT_ID_2
version: 2
Step 4: Configure Xcode Build Phaseโ
Add a "Run Script" build phase to your Xcode project:
- Open your project in Xcode
- Select your target
- Go to Build Phases tab
- Click + and select New Run Script Phase
- Name it "GenerateAnalytics"
- Add the following script:
# Detect EventPanel CLI installation
if command -v eventpanel &> /dev/null; then
eventpanel generate
else
echo "Error: EventPanel CLI not found. Please install it using: brew install eventpanel"
exit 1
fi
- Move the script phase before "Compile Sources"
- Ensure "For install builds only" is unchecked
- Specify Input Files as
$(SRCROOT)/EventPanel.yaml - Specify Output Files as
$(DERIVED_FILE_DIR)/DemoApp/Analytics/GeneratedAnalyticsEvents.swift
Step 5: Generate Analytics Codeโ
The code generation will run automatically during Xcode builds. You can also run it manually:
eventpanel generate
Usageโ
Basic Event Trackingโ
import Analytics
// Track a simple event
let event = AnalyticsEvents.ProfileScreen.profileScreenShown()
analytics.track(event)
// Track event with parameters
let onboardingEvent = AnalyticsEvents.onboardingScreenShown(origin: .facebook)
analytics.track(onboardingEvent)
// Track event with optional parameters
let loadingEvent = AnalyticsEvents.loadingScreenShown(cityId: "NYC")
analytics.track(loadingEvent)
Generated Code Structureโ
The CLI generates Swift code in the following structure:
DemoApp/
โโโ Analytics/
โ โโโ GeneratedAnalyticsEvents.swift # Generated event code
Example Generated Eventsโ
// Profile Screen Events
AnalyticsEvents.ProfileScreen.profileScreenShown()
AnalyticsEvents.ProfileScreen.profileScreenClosed()
// Onboarding Events with Enum
AnalyticsEvents.onboardingScreenShown(origin: Origin)
enum Origin {
case facebook
case google
case email
}
// Loading Events with Optional Parameters
AnalyticsEvents.loadingScreenShown(cityId: String?)
Configuration Referenceโ
EventPanel.yaml Structureโ
language: swift # Target language
plugin:
swiftgen:
documentation: true # Include documentation comments
generatedEventsPath: DemoApp/Analytics/GeneratedAnalyticsEvents.swift
eventTypeName: AnalyticsEvent # Base event type name
namespace: AnalyticsEvents # Namespace for generated code
events:
- id: EVENT_ID # Event ID from EventPanel
version: 1 # Event schema version
Adding Events Command Referenceโ
The eventpanel add command provides multiple ways to add events:
| Command | Description |
|---|---|
eventpanel add <event-id> | Add latest version of an event by ID |
eventpanel add <event-id> --version <version> | Add specific version of an event |
eventpanel add --name <event-name> | Add latest version of an event by name |
eventpanel add --all | Add all available events for the configured source |
eventpanel add --category-id <id> | Add all events from a category by ID |
eventpanel add --category-name <name> | Add all events from a category by name |
Options:
--scheme-update/--no-scheme-update: Control whether to apply scheme updates during generation (default: enabled)
Example Configurationโ
After running eventpanel init, your EventPanel.yaml will be created. You can then add events:
language: swift
plugin:
swiftgen:
documentation: true
generatedEventsPath: DemoApp/Analytics/GeneratedAnalyticsEvents.swift
eventTypeName: AnalyticsEvent
namespace: AnalyticsEvents
events:
- id: 7MJ5DRg_a7xrf8ZzeKmDb # Onboarding Screen Shown
version: 3
- id: iarZdDsKOfAmxMN3rfTtd # Loading Screen Shown
version: 4
- id: akn-KX7E0Of8d2L_PLBbW # Profile Screen Shown
version: 1
- id: Z0EhrWP91qyabKCziBJBQ # Profile Screen Closed
version: 1
Tip: You can also manage events through the EventPanel web interface, and they will be synced to your EventPanel.yaml file.
Project Structure Exampleโ
DemoApp/
โโโ Analytics/
โ โโโ AnalyticsEvent.swift # Base analytics event protocol
โ โโโ GeneratedAnalyticsEvents.swift # Generated event code
โโโ ContentView.swift # Main SwiftUI view
โโโ ContentViewModel.swift # View model with analytics
โโโ DemoAppApp.swift # App entry point
Authenticationโ
Setting API Tokenโ
The EventPanel CLI requires authentication to fetch event schemas from your workspace. After running eventpanel init, you must set your API token:
eventpanel set-token YOUR_API_TOKEN
How to get your API token:
- Log in to EventPanel web interface
- Navigate to your workspace settings
- Go to the API tokens section
- Create a new token
- Copy the token and use it with the
set-tokencommand
The token is securely stored locally and will be used for all authenticated CLI operations.
Verifying Authenticationโ
After setting the token, you can verify it works by running:
eventpanel generate
If authentication fails, you'll see an error message. Make sure:
- The token is valid and not expired
- The token has the necessary permissions for your workspace
- You're connected to the internet
Troubleshootingโ
CLI Not Foundโ
If you get command not found: eventpanel:
- Verify installation:
eventpanel --version - Check Homebrew:
brew list eventpanel - Check PATH:
echo $PATH - Restart Xcode after installation
Build Phase Errorsโ
- Script not running: Ensure the build phase is before "Compile Sources"
- Permission denied: Check script execution permissions
- Path issues: Verify
generatedEventsPathexists and is writable
Generation Errorsโ
- Invalid YAML: Check your
EventPanel.yamlsyntax - Missing Events: Ensure event IDs exist in your EventPanel workspace
- Swift version: Verify compatibility with your Swift version
Xcode Issuesโ
- Clean Build Folder: Product โ Clean Build Folder (โงโK)
- Derived Data: Delete derived data if issues persist
- Restart Xcode: Sometimes Xcode needs a restart after CLI installation
Best Practicesโ
- Version Control: Commit
EventPanel.yamlbut consider ignoring generated files - Build Integration: Use build phases for automatic generation
- Code Review: Review generated code changes when event schemas update
- Documentation: Use the
documentation: trueoption for inline docs - Testing: Test analytics events in your test suite
Advanced Usageโ
Custom Event Typesโ
The generated code supports custom types and enums:
// Events with custom enums
let event = AnalyticsEvents.onboardingScreenShown(origin: .facebook)
// Events with optional parameters
let loadingEvent = AnalyticsEvents.loadingScreenShown(cityId: nil)
Integration with Analytics SDKsโ
// Example with a custom analytics service
class AnalyticsService {
func track(_ event: AnalyticsEvent) {
// Convert to your analytics SDK format
yourAnalyticsSDK.track(event.name, parameters: event.parameters)
}
}
Learn Moreโ
- ๐ EventPanel CLI
- ๐ EventPanel Website
- ๐ iOS Documentation