matomo-org/matomo-sdk-ios
Über uns
Matomo-SDK für iOS, tvOS und macOS: ein in Swift geschriebener Matomo-Tracker
Web- und App-EntwicklungGemessener Sternzuwachs
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- +1
- in 1 month
- Verlauf wird gesammelt
Nettoänderungen in überlappenden Zeiträumen, keine Tagessummen. Fehlende Messwerte bleiben leer. Sterne zeigen Interesse, nicht Qualität.
README.md
OriginalMatomoTracker (former PiwikTracker) iOS SDK
The MatomoTracker is an iOS, tvOS and macOS SDK for sending app analytics to a Matomo server. MatomoTracker can be used from Swift and Objective-C.
Fancy help improve this SDK? Check this list to see what is left and can be improved.
Installation
The MatomoTracker is installed with the Swift Package Manager. Either use Xcode to add the package dependency or add the following dependency to your Package.swift:
.package(url: "https://github.com/matomo-org/matomo-sdk-ios.git", from: "v7.9.0"),
and in your dependencies:
.product(name: "MatomoTracker", package: "matomo-sdk-ios")
In every file you want to use the MatomoTracker, don't forget to import the framework with import MatomoTracker.
Releases up to 7.9 are also available via CocoaPods and Carthage.
Usage
Matomo Instance
The Matomo iOS SDK doesn't provide a instance of the PiwikTracker. In order to be able to track data you have to create an instance first.
let matomoTracker = MatomoTracker(siteID: "23", baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!)
The siteId is the ID that you can get if you add a website within the Matomo web interface. The baseURL it the URL to your Matomo web instance and has to include the "piwik.php" or "matomo.php" string.
You can either pass around this instance, or add an extension to the MatomoTracker class and add a shared instance property.
extension MatomoTracker {
static let shared: MatomoTracker = MatomoTracker(siteID: "1", baseURL: URL(string: "https://example.com/piwik.php")!)
}
The siteID is the ID that you can get if you add a website within the Matomo web interface. The baseURL is the URL to your Matomo web instance and has to include the "piwik.php" or "matomo.php" string.
You can use multiple instances within one application.
Opting Out
The MatomoTracker SDK supports opting out of tracking. Please use the isOptedOut property of the MatomoTracker to define if the user opted out of tracking.
matomoTracker.isOptedOut = true
Tracking Page Views
The MatomoTracker can track hierarchical screen names, e.g. screen/settings/register. Use this to create a hierarchical and logical grouping of screen views in the Matomo web interface.
matomoTracker.track(view: ["path","to","your","page"])
You can also set the url of the page.
let url = URL(string: "https://matomo.org/get-involved/")
matomoTracker.track(view: ["community","get-involved"], url: url)
Tracking Events
Events can be used to track user interactions such as taps on a button. An event consists of four parts:
- Category
- Action
- Name (optional, recommended)
- Value (optional)
matomoTracker.track(eventWithCategory: "player", action: "slide", name: "volume", value: 35.1)
This will log that the user slid the volume slider on the player to 35.1%.
Tracking search
The MatomoTracker can track how users use your app internal search. You can track what keywords were searched for, what categories they use, the number of results for a certain search and what searches resulted in no results.
matomoTracker.trackSearch(query: "Best mobile tracking", category: "Technology", resultCount: 15)
Tracking Outlinks
An outlink is an external URL the user has opened, for example in an in-app browser. The URL can have any scheme, like https, mailto or a custom app scheme.
matomoTracker.trackOutlink(URL(string: "https://matomo.org")!)
Custom Dimension
Using Custom Dimensions you can add properties to your tracking data, such as "Did the user finish the tutorial?", "Is the user a paying user?" or "Which version of the Application is being used?". Before sending custom dimensions please make sure Custom Dimensions are properly installed and configured. You will need the ID of your configured Dimension.
The scope of a Custom Dimension, either "Visit" or "Action", is configured in Matomo when creating the dimension. Events, searches and outlinks count as actions, so there is no separate scope for them.
Dimensions for every request (Visit Scope)
A dimension set on the MatomoTracker is sent along with every following request. This is best for Custom Dimensions in the Visit Scope.
matomoTracker.set(dimension: CustomDimension(index: 1, value: "1.0.0-beta2"))
To remove an already set dimension:
matomoTracker.remove(dimensionAtIndex: 1)
From Objective-C, use [matomoTracker setDimension:@"1.0.0-beta2" forIndex:1]. Custom Dimensions set on the MatomoTracker are not persisted by the SDK and have to be re-configured upon application startup. reset() removes them as well.
Dimensions for a single request (Action Scope)
To send a dimension only with a single page view, event, search or outlink, pass it to the tracking method. This is best for Custom Dimensions in the Action Scope.
matomoTracker.track(eventWithCategory: "shop", action: "tap", name: "upgrade_button", dimensions: [
CustomDimension(index: 3, value: "black_friday"),
CustomDimension(index: 4, value: "cta_primary"),
])
The dimensions parameter is available on track(view:url:dimensions:), track(eventWithCategory:action:name:value:dimensions:url:), trackSearch(query:category:resultCount:dimensions:url:), trackOutlink(_:dimensions:) and the Event initializer.
If a dimension with the same index is set on the MatomoTracker and passed to a tracking method, the value passed to the tracking method is used for that request.
Custom User ID
To add a custom User ID, simply set the value you'd like to use on the userId field of the tracker:
matomoTracker.userId = "coolUsername123"
All future events being tracked by the SDK will be associated with this userID, as opposed to the default UUID created for each Visitor.
Custom Visitor ID persisted on app starts
MatomoTracker will generate an _id upon first usage and will use this value to recognize the current visitor. This _id is persisted over app starts.
If you want to set your own visitor id, you can set your own visitor id with the forcedVisitorId field. Make sure you use a 16 character long hexadecimal string. The forcedVisitorId is persisted over app starts.
matomoTracker.forcedVisitorId = "0123456789abcdef"
Because the SDK persists this visitor id on app start, then we recommend to ask users for consent before tracking your app users.
Campaign Tracking
The Matomo iOS SDK supports campaign tracking.
matomoTracker.trackCampaign(name: "campaign_name", keyword: "campaign_keyword")
Content Tracking
The Matomo iOS SDK supports content tracking.
matomoTracker.trackContentImpression(name: "preview-liveaboard", piece: "Malaysia", target: "https://dummy.matomo.org/liveaboard/malaysia")
matomoTracker.trackContentInteraction(name: "preview-liveaboard", interaction: "tap", piece: "Malaysia", target: "https://dummy.matomo.org/liveaboard/malaysia")
Goal Tracking
The Matomo iOS SDK supports goal tracking.
matomoTracker.trackGoal(id: 1, revenue: 99.99)
Order Tracking
The Matomo iOS SDK supports order tracking.
let items = [
OrderItem(sku: "product_sku_1", name: "iPhone Xs", category: "phone", price: 999.99, quantity: 1),
OrderItem(sku: "product_sku_2", name: "iPhone Xs Max", category: "phone", price: 1199.99, quantity: 1)
]
matomoTracker.trackOrder(id: "order_id_1234", items: items, revenue: 2199.98, subTotal: 2000, tax: 190.98, shippingCost: 9)
Advanced Usage
Manual dispatching
The MatomoTracker will dispatch events every 30 seconds automatically. If you want to dispatch events manually, you can use the dispatch() function.
Session Management
The MatomoTracker starts a new session whenever the application starts. If you want to start a new session manually, you can use the startNewSession() function. You can, for example, start a new session whenever the user enters the application.
func applicationWillEnterForeground(_ application: UIApplication) {
matomoTracker.startNewSession()
}
Logging
The MatomoTracker per default logs warning and error messages to the console. You can change the LogLevel.
matomoTracker.logger = DefaultLogger(minLevel: .verbose)
matomoTracker.logger = DefaultLogger(minLevel: .debug)
matomoTracker.logger = DefaultLogger(minLevel: .info)
matomoTracker.logger = DefaultLogger(minLevel: .warning)
matomoTracker.logger = DefaultLogger(minLevel: .error)
You can also write your own Logger and send the logs wherever you want. Just write a new class/struct and let it conform to the Logger protocol.
Custom User Agent
The MatomoTracker will create a default user agent derived from the WKWebView user agent.
You can instantiate the MatomoTracker using your own user agent.
let matomoTracker = MatomoTracker(siteID: "5", baseURL: URL(string: "http://your.server.org/path-to-matomo/piwik.php")!, userAgent: "Your custom user agent")
Sending custom events
Instead of using the convenience functions for events and screen views for example you can create your event manually and even send custom tracking parameters. This feature isn't available from Objective-C.
func sendCustomEvent() {
guard let matomoTracker = MatomoTracker.shared else { return }
let downloadURL = URL(string: "https://builds.matomo.org/piwik.zip")!
let event = Event(tracker: matomoTracker, action: ["menu", "custom tracking parameters"], url: downloadURL, customTrackingParameters: ["download": downloadURL.absoluteString])
matomoTracker.track(event)
}
All custom events will be URL-encoded and dispatched along with the default Event parameters. Please read the Tracking API Documentation for more information on which parameters can be used.
Also: You cannot override Custom Parameter keys that are already defined by the Event itself. If you set those keys in the customTrackingParameters they will be discarded.
Automatic url generation
You can define the url property on every Event. If none is defined, the SDK will try to generate a url based on the contentBase of the MatomoTracker. If the contentBase is nil, no url will be generated. If the contentBase is set, it will append the actions of the event to it and use it as the url. A relative url without a scheme, like /mainPage/id.123, is appended to the contentBase as well. Per default the contentBase is generated using the application bundle identifier. For example http://org.matomo.skd. This will not result in resolvable urls, but enables the backend to analyse and structure them.
Event dispatching
Whenever you track an event or a page view it is stored in memory first. In every dispatch run a batch of those events are sent to the server. If the device is offline or the server doesn't respond these events will be kept and resent at a later time. Events currently aren't stored on disk and will be lost if the application is terminated. #137. You can use an experimental implementation of a Queue that stores all Events in the UserDefaults.
let queue = UserDefaultsQueue(userDefaults: UserDefaults.standard)
let dispatcher = URLSessionDispatcher(baseURL: URL(string: "https://example.com/matomo.php")!)
let matomoTracker = MatomoTracker(siteID: "1", queue: queue, dispatcher: dispatcher)
Contributing
Please read CONTRIBUTING.md for details.
License
MatomoTracker is available under the MIT license.
Ähnliche Projekte entdecken
Wird geladen…