Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom variables #205

Closed

Conversation

zantoku
Copy link

@zantoku zantoku commented Nov 23, 2017

This Pull Request adds support for Custom Variables, largely following the implementation pattern of Custom Dimensions. Custom Variables can be set scoped to visit (as a property of the shared PiwikTracker) or to an Event. Also, the tracker now generates the same default variables (Platform, OS Version, App Version) as the old 3.x tracker.

Also, I've improved the ObjC-interoperability with Custom Dimensions, by adding a setter method that can be called from there.

@zantoku zantoku mentioned this pull request Nov 23, 2017

extension PiwikTracker {

internal func getDefaultCVars() -> [CustomVariable] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about moving this to the CustomVariable struct? Something like this:

extension CustomVariable {
    static var defaultVariables: [CustomVariable] {
        // return ...
    }
}

@@ -36,6 +36,8 @@ final public class PiwikTracker: NSObject {

internal var dimensions: [CustomDimension] = []

@objc public var useDefaultCustomVariables: Bool = false
private lazy var cvars: [CustomVariable] = getDefaultCVars()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the cvars variable name here to much. We usually don't use abbreviations. What do you think about using customVariables here? This then shows another minor issue. It is imho hard to distinguish (just by the variable names) between the cvars and the customVariables that you implemented as a slice from these here.

What do you think about removing the useDefaultCustomVariables completely and let the users of the SDK add the "default" variables themselves? Exposing a defaultVariables property on the CustomVariable for example could help here. This way, the code here in the SDK is much cleaner and it is more direct for the user.

/// Note that, as with any array, the index of any succeeding Custom Variables is decremented by 1.
///
/// - Parameter index: The index that was previously returned by addCustomVariable().
@objc public func removeCustomVariableAtIndex(_ index: Int) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about referencing the variables by name, not by index? In this case I think the CustomVariable struct should bring the index to be referenced in the API.

@objc public func removeCustomVariable(withName name: String) {
    cvars = cvars.filter{$0.name != name }
}

var queryItems: [URLQueryItem] {
get {
let items = [
var items = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep this as a let and the join al the different query items together in the end?


private func cvarParameterValue() -> String {
var cvars: Array<String> = customVariables.enumerated().map { "\"\($0.offset + 1)\":[\"\($0.element.name)\",\"\($0.element.value)\"]" }
return "{\(cvars.joined(separator: ","))}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code is a bit to complex to parse. Do you think it is possible to optimize it?

@rafalkitta
Copy link

I would appreciate if you add this feature.

@brototyp
Copy link
Member

I will. There is only a few little things left. @zantoku let me know if you don't agree with some of my comments or if you can't find any time for them.

@brototyp
Copy link
Member

Hi @zantoku, thanks for your effort here. @manuroe improved your PR and we merged it in #223

@brototyp brototyp closed this Jan 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants