Skip to content

Commit

Permalink
Added Goal tracking (#273)
Browse files Browse the repository at this point in the history
* Added goal tracking

* Added URLQueryItems

* Extended changelog and readme

* Added new fields to EventFixture
  • Loading branch information
ThomasGoehringer authored and brototyp committed Sep 14, 2018
1 parent 47c842a commit 42e7718
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
* **improvement** Added public init to `CustomVariable` [#269](https://github.com/matomo-org/matomo-sdk-ios/issues/269)
* **improvement** Renamed the `visitorId` property to `userId` to be more inline with the Documentation. [#258](https://github.com/matomo-org/matomo-sdk-ios/issues/258)
* **improvement** Added a method for goal tracking [#272](https://github.com/matomo-org/matomo-sdk-ios/issues/272)
* **bugfix** Fixed an issue where `trackSearch` called from Objective-C would end in an infinite loop. [#263](https://github.com/matomo-org/matomo-sdk-ios/issues/263)
* **bugfix** Fixed an issue on Objective-C project where swift version was't set. [#260](https://github.com/matomo-org/matomo-sdk-ios/issues/260)

Expand Down
9 changes: 8 additions & 1 deletion MatomoTracker/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,15 @@ public struct Event {
let contentPiece: String?
let contentTarget: String?
let contentInteraction: String?

/// Goal tracking
/// https://matomo.org/docs/tracking-goals-web-analytics/
let goalId: Int?
let revenue: Float?
}

extension Event {
public init(tracker: MatomoTracker, action: [String], url: URL? = nil, referer: URL? = nil, eventCategory: String? = nil, eventAction: String? = nil, eventName: String? = nil, eventValue: Float? = nil, customTrackingParameters: [String:String] = [:], searchQuery: String? = nil, searchCategory: String? = nil, searchResultsCount: Int? = nil, dimensions: [CustomDimension] = [], variables: [CustomVariable] = [], contentName: String? = nil, contentInteraction: String? = nil, contentPiece: String? = nil, contentTarget: String? = nil) {
public init(tracker: MatomoTracker, action: [String], url: URL? = nil, referer: URL? = nil, eventCategory: String? = nil, eventAction: String? = nil, eventName: String? = nil, eventValue: Float? = nil, customTrackingParameters: [String:String] = [:], searchQuery: String? = nil, searchCategory: String? = nil, searchResultsCount: Int? = nil, dimensions: [CustomDimension] = [], variables: [CustomVariable] = [], contentName: String? = nil, contentInteraction: String? = nil, contentPiece: String? = nil, contentTarget: String? = nil, goalId: Int? = nil, revenue: Float? = nil) {
self.siteId = tracker.siteId
self.uuid = NSUUID()
self.visitor = tracker.visitor
Expand All @@ -117,5 +122,7 @@ extension Event {
self.contentPiece = contentPiece
self.contentTarget = contentTarget
self.contentInteraction = contentInteraction
self.goalId = goalId
self.revenue = revenue
}
}
3 changes: 3 additions & 0 deletions MatomoTracker/EventSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ fileprivate extension Event {
URLQueryItem(name: "c_p", value: contentPiece),
URLQueryItem(name: "c_t", value: contentTarget),
URLQueryItem(name: "c_i", value: contentInteraction),

URLQueryItem(name: "idgoal", value: goalId != nil ? "\(goalId!)" : nil),
URLQueryItem(name: "revenue", value: revenue != nil ? "\(revenue!)" : nil),
].filter { $0.value != nil }

let dimensionItems = dimensions.map { URLQueryItem(name: "dimension\($0.index)", value: $0.value) }
Expand Down
10 changes: 10 additions & 0 deletions MatomoTracker/MatomoTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ extension MatomoTracker {
let event = Event(tracker: self, action: [], url: url, eventCategory: category, eventAction: action, eventName: name, eventValue: value, dimensions: dimensions)
queue(event: event)
}

/// Tracks a goal as described here: https://matomo.org/docs/tracking-goals-web-analytics/
///
/// - Parameters:
/// - goalId: The defined ID of the Goal
/// - revenue: The monetary value that was generated by the Goal
public func track(goalWithId goalId: Int?, revenue: Float?) {
let event = Event(tracker: self, action: [], goalId: goalId, revenue: revenue)
queue(event: event)
}
}

extension MatomoTracker {
Expand Down
4 changes: 3 additions & 1 deletion MatomoTrackerTests/Fixtures/MemoryQueueFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ struct EventFixture {
contentName: nil,
contentPiece: nil,
contentTarget: nil,
contentInteraction: nil)
contentInteraction: nil,
goalId: nil,
revenue: nil)
}
}

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ matomoTracker.trackContentImpression(name: "preview-liveaboard", piece: "Malaysi
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](https://matomo.org/docs/tracking-goals-web-analytics/).

```Swift
matomoTracker.track(goalWithId: 1, revenue: 99.99)
```

## Advanced Usage
### Manual dispatching

Expand Down

0 comments on commit 42e7718

Please sign in to comment.