added iOS source code
[wl-app.git] / iOS / Pods / MatomoTracker / MatomoTracker / Event.swift
1 //
2 //  Event.swift
3 //  PiwikTracker
4 //
5 //  Created by Cornelius Horstmann on 21.12.16.
6 //  Copyright © 2016 PIWIK. All rights reserved.
7 //
8
9 import Foundation
10 import CoreGraphics
11
12 /// Represents an event of any kind.
13 ///
14 /// - Note: Should we store the resolution in the Event (cleaner) or add it before transmission (smaller)?
15 /// - Todo: 
16 ///     - Add Campaign Parameters: _rcn, _rck
17 ///     - Add Action info
18 ///     - Event Tracking info
19 ///     - Add Content Tracking info
20 ///     - Add Ecommerce info
21 ///
22 /// # Key Mapping:
23 /// Most properties represent a key defined at: [Tracking HTTP API](https://developer.piwik.org/api-reference/tracking-api). Keys that are not supported for now are:
24 ///
25 /// - idsite, rec, rand, apiv, res, cookie,
26 /// - All Plugins: fla, java, dir, qt, realp, pdf, wma, gears, ag
27 /// - cid: We will use the uid instead of the cid.
28 public struct Event {
29     let siteId: String
30     let uuid: NSUUID
31     let visitor: Visitor
32     let session: Session
33     
34     /// The Date and Time the event occurred.
35     /// api-key: h, m, s
36     let date: Date
37     
38     /// The full URL for the current action. 
39     /// api-key: url
40     let url: URL?
41     
42     /// api-key: action_name
43     let actionName: [String]
44     
45     /// The language of the device.
46     /// Should be in the format of the Accept-Language HTTP header field.
47     /// api-key: lang
48     let language: String
49     
50     /// Should be set to true for the first event of a session.
51     /// api-key: new_visit
52     let isNewSession: Bool
53     
54     /// Currently only used for Campaigns
55     /// api-key: urlref
56     let referer: URL?
57     let screenResolution : CGSize = Device.makeCurrentDevice().screenSize
58     
59     /// api-key: _cvar
60     let customVariables: [CustomVariable]
61     
62     /// Event tracking
63     /// https://piwik.org/docs/event-tracking/
64     let eventCategory: String?
65     let eventAction: String?
66     let eventName: String?
67     let eventValue: Float?
68     
69     /// Campaign tracking
70     /// https://matomo.org/docs/tracking-campaigns/
71     let campaignName: String?
72     let campaignKeyword: String?
73
74     /// Search tracking
75     /// api-keys: search, search_cat, search_count
76     let searchQuery: String?
77     let searchCategory: String?
78     let searchResultsCount: Int?
79     
80     let dimensions: [CustomDimension]
81     
82     let customTrackingParameters: [String:String]
83     
84     /// Content tracking
85     /// https://matomo.org/docs/content-tracking/
86     let contentName: String?
87     let contentPiece: String?
88     let contentTarget: String?
89     let contentInteraction: String?
90 }
91
92 extension Event {
93     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) {
94         self.siteId = tracker.siteId
95         self.uuid = NSUUID()
96         self.visitor = tracker.visitor
97         self.session = tracker.session
98         self.date = Date()
99         self.url = url ?? tracker.contentBase?.appendingPathComponent(action.joined(separator: "/"))
100         self.actionName = action
101         self.language = Locale.httpAcceptLanguage
102         self.isNewSession = tracker.nextEventStartsANewSession
103         self.referer = referer
104         self.eventCategory = eventCategory
105         self.eventAction = eventAction
106         self.eventName = eventName
107         self.eventValue = eventValue
108         self.searchQuery = searchQuery
109         self.searchCategory = searchCategory
110         self.searchResultsCount = searchResultsCount
111         self.dimensions = tracker.dimensions + dimensions
112         self.campaignName = tracker.campaignName
113         self.campaignKeyword = tracker.campaignKeyword
114         self.customTrackingParameters = customTrackingParameters
115         self.customVariables = tracker.customVariables + variables
116         self.contentName = contentName
117         self.contentPiece = contentPiece
118         self.contentTarget = contentTarget
119         self.contentInteraction = contentInteraction
120     }
121 }