Skip to content

Commit

Permalink
Added new ca parameter that is automatically sent along with every pa…
Browse files Browse the repository at this point in the history
…ge view (#355)

* Added new ca parameter that is automatically sent along with every page view

* Fixed Unit tests
  • Loading branch information
brototyp authored Oct 25, 2020
1 parent 5d03783 commit bbf6f12
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
* **improvement** Support new `ca` tracking parameter for tracking requests that aren't page views. [354](https://github.com/matomo-org/matomo-sdk-ios/issues/354)
* **improvement** Completely overhauled the UserAgent generation for automatic device and OS discovery. [#353](https://github.com/matomo-org/matomo-sdk-ios/pull/353)

## 7.2.2
Expand Down
9 changes: 8 additions & 1 deletion MatomoTracker/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public struct Event: Codable {
let visitor: Visitor
let session: Session

/// This flag defines if this event is a so called cutom action.
/// All events that are not a page view is a custom action.
/// api-key: ca
/// More info: https://github.com/matomo-org/matomo-sdk-ios/issues/354
let isCustomAction: Bool

/// The Date and Time the event occurred.
/// api-key: h, m, s
let date: Date
Expand Down Expand Up @@ -93,7 +99,7 @@ public struct Event: Codable {
}

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, goalId: Int? = nil, revenue: Float? = nil, orderId: String? = nil, orderItems: [OrderItem] = [], orderRevenue: Float? = nil, orderSubTotal: Float? = nil, orderTax: Float? = nil, orderShippingCost: Float? = nil, orderDiscount: Float? = nil, orderLastDate: Date? = 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, orderId: String? = nil, orderItems: [OrderItem] = [], orderRevenue: Float? = nil, orderSubTotal: Float? = nil, orderTax: Float? = nil, orderShippingCost: Float? = nil, orderDiscount: Float? = nil, orderLastDate: Date? = nil, isCustomAction: Bool = true) {
self.siteId = tracker.siteId
self.uuid = UUID()
self.visitor = tracker.visitor
Expand Down Expand Up @@ -130,5 +136,6 @@ extension Event {
self.orderShippingCost = orderShippingCost
self.orderDiscount = orderDiscount
self.orderLastDate = orderLastDate
self.isCustomAction = isCustomAction
}
}
1 change: 1 addition & 0 deletions MatomoTracker/EventAPISerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fileprivate extension Event {
let items = [
URLQueryItem(name: "idsite", value: siteId),
URLQueryItem(name: "rec", value: "1"),
URLQueryItem(name: "ca", value: isCustomAction ? "1" : nil),
// Visitor
URLQueryItem(name: "_id", value: visitor.id),
URLQueryItem(name: "cid", value: visitor.forcedId),
Expand Down
2 changes: 1 addition & 1 deletion MatomoTracker/MatomoTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ extension MatomoTracker {
/// - Parameter url: The optional url of the page that was viewed.
/// - Parameter dimensions: An optional array of dimensions, that will be set only in the scope of this view.
public func track(view: [String], url: URL? = nil, dimensions: [CustomDimension] = []) {
let event = Event(tracker: self, action: view, url: url, dimensions: dimensions)
let event = Event(tracker: self, action: view, url: url, dimensions: dimensions, isCustomAction: false)
queue(event: event)
}

Expand Down
65 changes: 38 additions & 27 deletions MatomoTrackerTests/Fixtures/MemoryQueueFixtures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,44 @@ struct EventFixture {
static func event() -> Event {
let visitor = Visitor(id: "spec_visitor_id", forcedId: nil, userId: "spec_user_id")
let session = Session(sessionsCount: 0, lastVisit: Date(), firstVisit: Date())
return Event(uuid: UUID(), siteId: "spec_1", visitor: visitor, session: session, date: Date(), url: URL(string: "http://spec_url")!, actionName: ["spec_action"], language: "spec_language", isNewSession: true, referer: nil,
customVariables: [],
eventCategory: nil,
eventAction: nil,
eventName: nil,
eventValue: nil,
campaignName: nil,
campaignKeyword: nil,
searchQuery: nil,
searchCategory: nil,
searchResultsCount: nil,
dimensions: [],
customTrackingParameters: [:],
contentName: nil,
contentPiece: nil,
contentTarget: nil,
contentInteraction: nil,
goalId: nil,
revenue: nil,
orderId: nil,
orderItems: [],
orderRevenue: nil,
orderSubTotal: nil,
orderTax: nil,
orderShippingCost: nil,
orderDiscount: nil,
orderLastDate: nil)
return Event(
uuid: UUID(),
siteId: "spec_1",
visitor: visitor,
session: session,
isCustomAction: true,
date: Date(),
url: URL(string: "http://spec_url")!,
actionName: ["spec_action"],
language: "spec_language",
isNewSession: true,
referer: nil,
customVariables: [],
eventCategory: nil,
eventAction: nil,
eventName: nil,
eventValue: nil,
campaignName: nil,
campaignKeyword: nil,
searchQuery: nil,
searchCategory: nil,
searchResultsCount: nil,
dimensions: [],
customTrackingParameters: [:],
contentName: nil,
contentPiece: nil,
contentTarget: nil,
contentInteraction: nil,
goalId: nil,
revenue: nil,
orderId: nil,
orderItems: [],
orderRevenue: nil,
orderSubTotal: nil,
orderTax: nil,
orderShippingCost: nil,
orderDiscount: nil,
orderLastDate: nil)
}
}

Expand Down

0 comments on commit bbf6f12

Please sign in to comment.