Giter VIP home page Giter VIP logo

anyanalytics's Introduction

AnyAnalytics

build status

A analytics wrapper inspired by Moya that allows you to easily change the analytics provider you want to use.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

AnyAnalytics is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "AnyAnalytics", :git => 'https://github.com/swarmnyc/AnyAnalytics'

Example

//
// organize the data you are tracking all in one place or split tracking up into different sections
//

enum AnalyticsEvents {
	case navigatedToScreen(screen: TrackedScreen)
	case pressedButton(buttonName: String)
	case watchedVideo(amountWatched: Double, videoId: String)
}

enum AnalyticsEvents: TrackerEvent {
	public var eventName: String {
		switch self {
		case .navigatedToScreen(let screen):
			return "Screen View \(screen.analyticsName)"
		case .pressedButton(let buttonName):
			return "buttonPress:\(buttonName)"
		case .watchedVideo( _, let videoId):
			return "watchedVideo:\(videoId)"
		}
	}
	
	public var metaData: [String: Any]? {
		switch self {
			case .navigatedToScreen(let screen):
				return nil
			case .pressedButton( _):
				return nil
			case .watchedVideo(let amountWatched, _):
				return ["amountWatched": amountWatched]
		}
	}
	
	public var type: EventTypes {
		switch self {
			case .navigatedToScreen( _):
				return .screen
			case .pressedButton( _):
				return .event
			case .watchedFullVideo( _):
				return .event
		}
	}
}

//
// Call an event like this:
//

AnalyticsEvent.pressedButton(buttonName: "login").track(withTracker: SegmentIOTracker())

//
//A blank tracker for testing (doesn't send data anywhere)
//

public class EmptyTracker: TrackerImplementation {

     open func trackScreen(_ event: TrackerEvent) {
        
    }
    
    open func trackEvent(_ event: TrackerEvent) {
        
    }
    
    open func timeEvent(_ event: TrackerEvent) {
        
    }
    
    open func customTracking(_ event: TrackerEvent) {
        
    }
    
}

//an example SegmentIO tracker 


class SegmentIOTracker: TrackerImplementation {
    
    init(withWriteKey writeKey: String) {
        let configuration = SEGAnalyticsConfiguration(writeKey: writeKey)
        configuration.trackApplicationLifecycleEvents = true
        SEGAnalytics.setup(with: configuration)
    }
    
    func trackScreen(_ event: TrackerEvent) {
        SEGAnalytics.shared().screen(event.eventName, properties: event.metaData)
    }
    
    func trackEvent(_ event: TrackerEvent) {
        SEGAnalytics.shared().track(event.eventName, properties: event.metaData)
    }
    
    func timeEvent(_ event: TrackerEvent) {
        //didn't implement this
    }
    
    func customTracking(_ event: TrackerEvent) {
        //didn't implement this, use any other type of tracking they offer here
    }
}

Author

ahartwel, [email protected]

License

AnyAnalytics is available under the MIT license. See the LICENSE file for more info.

anyanalytics's People

Contributors

ahartwel avatar

Watchers

Cap'n Coconut avatar James Cloos avatar Valerie avatar Tao Ren avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.