Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Added activation tracking #148

Merged
merged 1 commit into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ClassySharkWS/src/com/google/classyshark/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.classyshark;

import com.google.classyshark.analytics.Analytics;
import com.google.classyshark.cli.CliMode;
import com.google.classyshark.gui.GuiMode;

Expand All @@ -37,6 +38,8 @@ private static boolean isGui(List<String> argsAsArray) {
public static void main(final String[] args) {
final List<String> argsAsArray = Arrays.asList(args);

Analytics.INSTANCE.addActivation();

if (isGui(argsAsArray)) {
GuiMode.with(argsAsArray);
} else {
Expand Down
34 changes: 34 additions & 0 deletions ClassySharkWS/src/com/google/classyshark/analytics/Analytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2017 Google, Inc.
*
* 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.
*/

package com.google.classyshark.analytics;

import com.google.classyshark.Version;

// based on https://github.com/siddii/jgoogleanalytics
public enum Analytics {
INSTANCE;

public void addActivation() {
JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker(
"ClassyShark-Activation",
Version.MAJOR + "." + Version.MINOR,
"UA-91889970-1");

FocusPoint focusPoint = new FocusPoint("Activation");
tracker.trackAsynchronously(focusPoint);
}
}
97 changes: 97 additions & 0 deletions ClassySharkWS/src/com/google/classyshark/analytics/FocusPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2015 Siddique Hameed
*
* 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.
*/

package com.google.classyshark.analytics;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
* Focus point of the application. It can represent data points like application load, application module load, user actions, error events etc.
*
* @author : Siddique Hameed
* @version : 0.1
*/

public class FocusPoint {

private String name;
private FocusPoint parentFocusPoint;
private static final String URI_SEPARATOR = "/";
private static final String TITLE_SEPARATOR = "-";

public FocusPoint(String name) {
this.name = name;
}

public FocusPoint(String name, FocusPoint parentFocusPoint) {
this(name);
this.parentFocusPoint = parentFocusPoint;
}

public String getName() {
return name;
}


public void setParentTrackPoint(FocusPoint parentFocusPoint) {
this.parentFocusPoint = parentFocusPoint;
}

public FocusPoint getParentFocusPoint() {
return parentFocusPoint;
}

public String getContentURI() {
StringBuffer contentURIBuffer = new StringBuffer();
getContentURI(contentURIBuffer, this);
return contentURIBuffer.toString();
}

public String getContentTitle() {
StringBuffer titleBuffer = new StringBuffer();
getContentTitle(titleBuffer, this);
return titleBuffer.toString();
}

private void getContentURI(StringBuffer contentURIBuffer, FocusPoint focusPoint) {
FocusPoint parentFocuPoint = focusPoint.getParentFocusPoint();

if (parentFocuPoint != null) {
getContentURI(contentURIBuffer, parentFocuPoint);
}
contentURIBuffer.append(URI_SEPARATOR);
contentURIBuffer.append(encode(focusPoint.getName()));
}

private String encode(String name) {
try {
return URLEncoder.encode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
return name;
}
}

private void getContentTitle(StringBuffer titleBuffer, FocusPoint focusPoint) {
FocusPoint parentFocusPoint = focusPoint.getParentFocusPoint();

if (parentFocusPoint != null) {
getContentTitle(titleBuffer, parentFocusPoint);
titleBuffer.append(TITLE_SEPARATOR);
}
titleBuffer.append(encode(focusPoint.getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2015 Siddique Hameed
*
* 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.
*/

package com.google.classyshark.analytics;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;

/**
* URL building logic for the earlier versions of google analytics (urchin.js)
*
* @author : Siddique Hameed
* @version : 0.1
*/

public class GoogleAnalytics_v1_URLBuildingStrategy implements URLBuildingStrategy {
private FocusPoint appFocusPoint;
private String googleAnalyticsTrackingCode;
private String refererURL = "http://www.BoxySystems.com";

private static final String TRACKING_URL_Prefix = "http://www.google-analytics.com/__utm.gif";

private static final Random random = new Random();
private static String hostName = "localhost";

static {
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
//ignore this
}
}


public GoogleAnalytics_v1_URLBuildingStrategy(String appName, String googleAnalyticsTrackingCode) {
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
this.appFocusPoint = new FocusPoint(appName);
}

public GoogleAnalytics_v1_URLBuildingStrategy(String appName, String appVersion, String googleAnalyticsTrackingCode) {
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
this.appFocusPoint = new FocusPoint(appVersion, new FocusPoint(appName));
}


public String buildURL(FocusPoint focusPoint) {

int cookie = random.nextInt();
int randomValue = random.nextInt(2147483647) - 1;
long now = new Date().getTime();

// String $urchinUrl="http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp." +
// "'&utmac='.$var_utmac.'" +
// "&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B";


focusPoint.setParentTrackPoint(appFocusPoint);
StringBuffer url = new StringBuffer(TRACKING_URL_Prefix);
url.append("?utmwv=1"); //Urchin/Analytics version
url.append("&utmn=" + random.nextInt());
url.append("&utmcs=UTF-8"); //document encoding
url.append("&utmsr=1440x900"); //screen resolution
url.append("&utmsc=32-bit"); //color depth
url.append("&utmul=en-us"); //user language
url.append("&utmje=1"); //java enabled
url.append("&utmfl=9.0%20%20r28"); //flash
url.append("&utmcr=1"); //carriage return
url.append("&utmdt=" + focusPoint.getContentTitle()); //The optimum keyword density //document title
url.append("&utmhn=" + hostName);//document hostname
url.append("&utmr=" + refererURL); //referer URL
url.append("&utmp=" + focusPoint.getContentURI());//document page URL
url.append("&utmac=" + googleAnalyticsTrackingCode);//Google Analytics account
url.append("&utmcc=__utma%3D'" + cookie + "." + randomValue + "." + now + "." + now + "." + now + ".2%3B%2B__utmb%3D" + cookie + "%3B%2B__utmc%3D" + cookie + "%3B%2B__utmz%3D" + cookie + "." + now + ".2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D" + cookie);
return url.toString();
}

public void setRefererURL(String refererURL) {
this.refererURL = refererURL;
}
}
102 changes: 102 additions & 0 deletions ClassySharkWS/src/com/google/classyshark/analytics/HTTPGetMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright 2015 Siddique Hameed
*
* 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.
*/

package com.google.classyshark.analytics;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Simple class peforming HTTP Get method on the requested url
*
* @author : Siddique Hameed
* @version : 0.1
*/

public class HTTPGetMethod {
private static final String GET_METHOD_NAME = "GET";

private static final String SUCCESS_MESSAGE = "JGoogleAnalytics: Tracking Successful!";

private LoggingAdapter loggingAdapter = null;

public void setLoggingAdapter(LoggingAdapter loggingAdapter) {
this.loggingAdapter = loggingAdapter;
}

private static String uaName = null; // User Agent name

private static String osString = "Unknown";

HTTPGetMethod() {
// Initialise the static parameters if we need to.
if (uaName == null) {
uaName = "Java/" + System.getProperty("java.version"); // java version info appended
// os string is architecture+osname+version concatenated with _
osString = System.getProperty("os.arch");
if (osString == null || osString.length() < 1) {
osString = "";
} else {
osString += "; ";
osString += System.getProperty("os.name") + " "
+ System.getProperty("os.version");
}
}
}

public void request(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection urlConnection = openURLConnection(url);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestMethod(GET_METHOD_NAME);
urlConnection.setRequestProperty("User-agent", uaName + " ("
+ osString + ")");

urlConnection.connect();
int responseCode = getResponseCode(urlConnection);
if (responseCode != HttpURLConnection.HTTP_OK) {
logError("JGoogleAnalytics: Error tracking, url=" + urlString);
} else {
logMessage(SUCCESS_MESSAGE);
}
} catch (Exception e) {
logError(e.getMessage());
}
}

protected int getResponseCode(HttpURLConnection urlConnection)
throws IOException {
return urlConnection.getResponseCode();
}

private HttpURLConnection openURLConnection(URL url) throws IOException {
return (HttpURLConnection) url.openConnection();
}

private void logMessage(String message) {
if (loggingAdapter != null) {
loggingAdapter.logMessage(message);
}
}

private void logError(String errorMesssage) {
if (loggingAdapter != null) {
loggingAdapter.logError(errorMesssage);
}
}
}
Loading