https://github.com/simpleanalytics/swift-package
https://github.com/simpleanalytics/swift-package
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/simpleanalytics/swift-package
- Owner: simpleanalytics
- License: mit
- Created: 2023-10-31T11:19:02.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-04T14:44:16.000Z (over 1 year ago)
- Last Synced: 2025-08-18T14:55:07.966Z (10 months ago)
- Language: Swift
- Homepage: https://docs.simpleanalytics.com/install-simple-analytics-with-swift
- Size: 61.5 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Analytics Swift Package
Swift package for [Simple Analytics](https://www.simpleanalytics.com/?referral=roel-van-der-kraan). Add privacy friendly analytics to your iOS apps. Currently in development. This is an alpha version, everything might change.
[](https://swiftpackageindex.com/simpleanalytics/swift-package)
[](https://swiftpackageindex.com/simpleanalytics/swift-package)
## Installation
When using Xcode to add the package dependency add this repository via `File` > `Add package dependency`:
`https://github.com/simpleanalytics/swift-package.git`
Or when working with a package manifest use:
```swift
.package(url: "https://github.com/simpleanalytics/swift-package.git", from: "0.3.0")
```
## Usage
You'll need a Simple Analytics account to be able to use this package. See [Simple Analytics](https://www.simpleanalytics.com/?referral=roel-van-der-kraan) for more info.
Import the library:
```swift
import SimpleAnalytics
```
You will need the hostname of a Simple Analytics website to start an instance of `SimpleAnalytics` in your app. You can add a fake "website" to Simple Analytics specifically for your app. There is no need to point to a real server. You can use something like `mobileapp.yourdomain.com`. Skip the HTML validation by clicking "I installed the script".
⚠️ Make sure the hostname you set in Swift matches the website domain name in Simple Analytics (without http:// or https://).
```swift
let simpleAnalytics = SimpleAnalytics(hostname: "mobileapp.yourdomain.com")
```
You can create an instance where you need it, or you can make an extension and use it as a static class.
```swift
import SimpleAnalytics
extension SimpleAnalytics {
static let shared: SimpleAnalytics = SimpleAnalytics(hostname: "mobileapp.yourdomain.com")
}
```
## Tracking
You can call the tracking functions from anywhere in your app.
### Tracking Pageviews
Use pageviews to track screens in your app.
```swift
SimpleAnalytics.shared.track(path: ["list"])
```
To represent a hierarchy in your views, add every level as an entry in the path array:
```swift
SimpleAnalytics.shared.track(path: ["detailview", "item1", "edit"])
```
This will be converted to a pageview on `/detailview/item1/edit` on Simple Analytics.
### Tracking Events
Use events to track interactions or noticable events like errors or success on a page.
```swift
SimpleAnalytics.shared.track(event: "logged in")
```
You can provide an optional path to track alongside the event.
```swift
SimpleAnalytics.shared.track(event: "logged in", path: ["login", "social"])
```
### Tracking Visitors for Apps + Widgets
If you have an app + widget(s), by default one visit per day per target (app, widget) is treated as separate 'visitors'. This means that if you have an app with a small and medium widget, one person using the core app and both widgets will show in your SimpleAnalytics dashboard as 3 visitors. However, you can ensure one visitor only appears once per day by creating an App Group in your Xcode project. Create a new App Group ([instructions](https://developer.apple.com/documentation/xcode/configuring-app-groups)) in each of your targets (Project > Targets > Signing & Capabilities > App Groups) with the same name. Use this app group name in your SimpleAnalytics instance.
```swift
let simpleAnalytics = SimpleAnalytics(hostname: "app.simpleanalytics.com", sharedDefaultsSuiteName: "group.com.simpleanlytics.app")
```
## Examples
### SwiftUI example
In SwiftUI, a good place to put the Pageview tracking code is in your view `.onAppear{}` modifier.
```swift
import SwiftUI
import SimpleAnalytics
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
.onAppear {
SimpleAnalytics.shared.track(path: ["example"])
}
}
}
```
### UIKit example
When using UIKit, you can put Pageview tracking in `viewDidAppear()`
```swift
import UIKit
import SimpleAnalytics
class ExampleViewController: UITableViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
SimpleAnalytics.shared.track(path: ["example"])
}
}
```