Skip to content

Commit

Permalink
iOS Files and library
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuellemm committed Aug 5, 2014
1 parent 7509aaf commit a6a231a
Show file tree
Hide file tree
Showing 11 changed files with 1,287 additions and 0 deletions.
234 changes: 234 additions & 0 deletions source/Plugins/GoogleAnalyticsV3/GAIHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using UnityEngine;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

/*
Wrapper for Objective-C iOS SDK
*/
public class GAIHandler {
#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void setName(string name);
public void _setName(string name){
setName(name);
}

[DllImport("__Internal")]
private static extern void setOptOut(bool optOut);
public void _setOptOut(bool optOut){
setOptOut(optOut);
}

[DllImport("__Internal")]
private static extern void setDispatchInterval(int time);
public void _setDispatchInterval(int time){
setDispatchInterval(time);
}

[DllImport("__Internal")]
private static extern void anonymizeIP();
public void _anonymizeIP(){
anonymizeIP();
}

[DllImport("__Internal")]
private static extern void setTrackUncaughtExceptions(bool trackUncaughtExceptions);
public void _setTrackUncaughtExceptions(bool trackUncaughtExceptions){
setTrackUncaughtExceptions(trackUncaughtExceptions);
}

[DllImport("__Internal")]
private static extern void setDryRun(bool dryRun);
public void _setDryRun(bool dryRun){
setDryRun(dryRun);
}

[DllImport("__Internal")]
private static extern void setSampleFrequency(float sampleFrequency);
public void _setSampleFrequency(float sampleFrequency){
setSampleFrequency(sampleFrequency);
}

[DllImport("__Internal")]
private static extern void setLogLevel(int logLevel);
public void _setLogLevel(int logLevel){
setLogLevel(logLevel);
}

[DllImport("__Internal")]
private static extern void startSession();
public void _startSession(){
startSession();
}

[DllImport("__Internal")]
private static extern void stopSession();
public void _stopSession(){
stopSession();
}

[DllImport("__Internal")]
private static extern IntPtr trackerWithName(string name, string trackingId);
public IntPtr _getTrackerWithName(string name, string trackingId){
return trackerWithName(name, trackingId);
}

[DllImport("__Internal")]
private static extern IntPtr trackerWithTrackingId(string trackingId);
public IntPtr _getTrackerWithTrackingId(string trackingId){
return trackerWithTrackingId(trackingId);
}

[DllImport("__Internal")]
private static extern void set(string parameterName, string value);
public void _set(string parameterName, object value){
set(parameterName, value.ToString());
}

[DllImport("__Internal")]
private static extern void setBool(string parameterName, bool value);
public void _setBool(string parameterName, bool value){
setBool(parameterName, value);
}

[DllImport("__Internal")]
private static extern string get(string parameterName);
public string _get(string parameterName){
return get(parameterName);
}

[DllImport("__Internal")]
private static extern void send(string parametersString);
public void _send(Dictionary<string, string> parameters){
string parametersString = "";
foreach(KeyValuePair<string, string> kvp in parameters)
{
parametersString += kvp.Key + "=" + kvp.Value + "\n";
}
send(parametersString);
}

[DllImport("__Internal")]
private static extern void dispatch();
public void _dispatchHits(){
dispatch();
}

[DllImport("__Internal")]
private static extern void sendAppView(string screenName);
public void _sendAppView(AppViewHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendAppView(builder.GetScreenName());
}

[DllImport("__Internal")]
private static extern void sendEvent(string category, string action, string label, long value);
public void _sendEvent(EventHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendEvent(builder.GetEventCategory(), builder.GetEventAction(), builder.GetEventLabel(), builder.GetEventValue());
}

//NOT TESTED
[DllImport("__Internal")]
private static extern void sendTransaction(string transactionID, string affiliation, double revenue, double tax, double shipping, string currencyCode);
public void _sendTransaction(TransactionHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendTransaction(builder.GetTransactionID(), builder.GetAffiliation(), builder.GetRevenue(), builder.GetTax(), builder.GetShipping(), builder.GetCurrencyCode());
}

[DllImport("__Internal")]
private static extern void sendItemWithTransaction(string transactionID, string name, string sku, string category, double price, long quantity, string currencyCode);
public void _sendItemWithTransaction(ItemHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendItemWithTransaction(builder.GetTransactionID(), builder.GetName(), builder.GetSKU(), builder.GetCategory(), builder.GetPrice(), builder.GetQuantity(),builder.GetCurrencyCode());
}

[DllImport("__Internal")]
private static extern void sendException(string exceptionDescription, bool isFatal);
public void _sendException(ExceptionHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendException(builder.GetExceptionDescription(), builder.IsFatal());
}

[DllImport("__Internal")]
private static extern void sendSocial(string socialNetwork, string socialAction, string targetUrl);
public void _sendSocial(SocialHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendSocial(builder.GetSocialNetwork(), builder.GetSocialAction(), builder.GetSocialTarget());
}

[DllImport("__Internal")]
private static extern void sendTiming(string timingCategory,long timingInterval, string timingName, string timingLabel);
public void _sendTiming(TimingHitBuilder builder){
_setCustomDimensionOnTracker(builder);
_setCustomMetricOnTracker(builder);
_buildCampaignParametersDictionary(builder);
sendTiming(builder.GetTimingCategory(), builder.GetTimingInterval(), builder.GetTimingName(), builder.GetTimingLabel());
}

[DllImport("__Internal")]
private static extern void setCustomDimensionOnTracker(int index, string value);
public void _setCustomDimensionOnTracker<T>(HitBuilder<T> builder){
foreach(KeyValuePair<int, string> entry in builder.GetCustomDimensions())
{
setCustomDimensionOnTracker(entry.Key, entry.Value);
}
}

[DllImport("__Internal")]
private static extern void setCustomMetricOnTracker(int index, string value);
public void _setCustomMetricOnTracker<T>(HitBuilder<T> builder){
foreach(KeyValuePair<int, string> entry in builder.GetCustomMetrics())
{
setCustomMetricOnTracker(entry.Key, entry.Value);
}
}

[DllImport("__Internal")]
private static extern void buildCampaignParametersDictionary(string source, string medium, string name, string content, string keyword);
public void _buildCampaignParametersDictionary<T>(HitBuilder<T> builder){
if(!String.IsNullOrEmpty(builder.GetCampaignSource())){
buildCampaignParametersDictionary(builder.GetCampaignSource(),
builder.GetCampaignMedium() != null ? builder.GetCampaignMedium() : "",
builder.GetCampaignName() != null? builder.GetCampaignName() : "",
builder.GetCampaignContent() != null? builder.GetCampaignContent() : "",
builder.GetCampaignKeyword() != null? builder.GetCampaignKeyword() : "");
} else if(!String.IsNullOrEmpty(builder.GetCampaignMedium()) ||
!String.IsNullOrEmpty(builder.GetCampaignName()) ||
!String.IsNullOrEmpty(builder.GetCampaignMedium()) ||
!String.IsNullOrEmpty(builder.GetCampaignContent()) ||
!String.IsNullOrEmpty(builder.GetCampaignKeyword())) {
Debug.Log("A required parameter (campaign source) is null or empty. No campaign parameters will be added to hit.");
}
}
#endif
}
164 changes: 164 additions & 0 deletions source/Plugins/GoogleAnalyticsV3/GoogleAnalyticsiOSV3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using UnityEngine;
using System;
using System.Collections.Generic;

/*
GoogleAnalyticsiOSV3 handles building hits using the iOS SDK.
Developers should call the methods in GoogleAnalyticsV3, which will call the
appropriate methods in this class if the application is built for iOS.
*/
public class GoogleAnalyticsiOSV3 {

#if UNITY_IPHONE && !UNITY_EDITOR
private string trackingCode;
private string appName;
private string bundleIdentifier;
private string appVersion;
private int dispatchPeriod;
private float sampleFrequency;
private GoogleAnalyticsV3.DebugMode logLevel;
private bool anonymizeIP;
private bool dryRun;
private GAIHandler handler;

internal void InitializeTracker () {
Debug.Log ("Initializing Google Analytics iOS Tracker.");
handler = new GAIHandler();
handler._setDispatchInterval(dispatchPeriod);
handler._setDryRun(dryRun);
handler._setSampleFrequency(sampleFrequency);
handler._setTrackUncaughtExceptions(true);
SetTrackerVal(Fields.APP_NAME, appName);
SetTrackerVal(Fields.APP_ID, bundleIdentifier);
SetTrackerVal(Fields.APP_VERSION, appVersion);
if(anonymizeIP) {
handler._anonymizeIP();
}
SetLogLevel(logLevel);
handler._getTrackerWithTrackingId(trackingCode);
}

private void SetLogLevel(GoogleAnalyticsV3.DebugMode logLevel) {
switch(logLevel)
{
case GoogleAnalyticsV3.DebugMode.ERROR:
handler._setLogLevel(1);
break;
case GoogleAnalyticsV3.DebugMode.VERBOSE:
handler._setLogLevel(4);
break;
case GoogleAnalyticsV3.DebugMode.INFO:
handler._setLogLevel(3);
break;
default:
handler._setLogLevel(2);
break;
}
}

public void ClearUserIDOverride() {
SetTrackerVal(Fields.USER_ID, null);
}

public void DispatchHits(){
handler._dispatchHits();
}

public void StartSession(){
handler._startSession();
}

public void StopSession(){
handler._stopSession();
}

public void LogScreen(AppViewHitBuilder builder){
handler._sendAppView(builder);
}

public void LogEvent(EventHitBuilder builder){
handler._sendEvent(builder);
}

internal void LogTransaction(TransactionHitBuilder builder) {
handler._sendTransaction(builder);
}

internal void LogItem(ItemHitBuilder builder) {
handler._sendItemWithTransaction(builder);
}

public void LogException(ExceptionHitBuilder builder) {
handler._sendException(builder);
}

public void LogSocial(SocialHitBuilder builder) {
handler._sendSocial(builder);
}

public void LogTiming(TimingHitBuilder builder) {
handler._sendTiming(builder);
}

public void SetOptOut(bool optOut) {
handler._setOptOut(optOut);
}

public void SetTrackerVal(Field fieldName, object value){
handler._set(fieldName.ToString(), value);
}

public void SetTrackingCode(string trackingCode) {
this.trackingCode = trackingCode;
}

public void SetAppName(string appName) {
this.appName = appName;
}

public void SetBundleIdentifier(string bundleIdentifier) {
this.bundleIdentifier = bundleIdentifier;
}

public void SetAppVersion(string appVersion) {
this.appVersion = appVersion;
}

public void SetDispatchPeriod(int dispatchPeriod) {
this.dispatchPeriod = dispatchPeriod;
}

public void SetSampleFrequency(float sampleFrequency) {
this.sampleFrequency = sampleFrequency;
}

public void SetLogLevelValue(GoogleAnalyticsV3.DebugMode logLevel) {
this.logLevel = logLevel;
}

public void SetAnonymizeIP(bool anonymizeIP) {
this.anonymizeIP = anonymizeIP;
}

public void SetDryRun(bool dryRun) {
this.dryRun = dryRun;
}

#endif
}
Loading

0 comments on commit a6a231a

Please sign in to comment.