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

Commit 2a9ca59

Browse files
authored
Merge pull request #148 from google/daily-activations
Added activation tracking
2 parents ca5f718 + ba158e7 commit 2a9ca59

File tree

8 files changed

+513
-0
lines changed

8 files changed

+513
-0
lines changed

ClassySharkWS/src/com/google/classyshark/Main.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.classyshark;
1818

19+
import com.google.classyshark.analytics.Analytics;
1920
import com.google.classyshark.cli.CliMode;
2021
import com.google.classyshark.gui.GuiMode;
2122

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

41+
Analytics.INSTANCE.addActivation();
42+
4043
if (isGui(argsAsArray)) {
4144
GuiMode.with(argsAsArray);
4245
} else {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2017 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.analytics;
18+
19+
import com.google.classyshark.Version;
20+
21+
// based on https://github.com/siddii/jgoogleanalytics
22+
public enum Analytics {
23+
INSTANCE;
24+
25+
public void addActivation() {
26+
JGoogleAnalyticsTracker tracker = new JGoogleAnalyticsTracker(
27+
"ClassyShark-Activation",
28+
Version.MAJOR + "." + Version.MINOR,
29+
"UA-91889970-1");
30+
31+
FocusPoint focusPoint = new FocusPoint("Activation");
32+
tracker.trackAsynchronously(focusPoint);
33+
}
34+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2015 Siddique Hameed
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.analytics;
18+
19+
import java.io.UnsupportedEncodingException;
20+
import java.net.URLEncoder;
21+
22+
/**
23+
* Focus point of the application. It can represent data points like application load, application module load, user actions, error events etc.
24+
*
25+
* @author : Siddique Hameed
26+
* @version : 0.1
27+
*/
28+
29+
public class FocusPoint {
30+
31+
private String name;
32+
private FocusPoint parentFocusPoint;
33+
private static final String URI_SEPARATOR = "/";
34+
private static final String TITLE_SEPARATOR = "-";
35+
36+
public FocusPoint(String name) {
37+
this.name = name;
38+
}
39+
40+
public FocusPoint(String name, FocusPoint parentFocusPoint) {
41+
this(name);
42+
this.parentFocusPoint = parentFocusPoint;
43+
}
44+
45+
public String getName() {
46+
return name;
47+
}
48+
49+
50+
public void setParentTrackPoint(FocusPoint parentFocusPoint) {
51+
this.parentFocusPoint = parentFocusPoint;
52+
}
53+
54+
public FocusPoint getParentFocusPoint() {
55+
return parentFocusPoint;
56+
}
57+
58+
public String getContentURI() {
59+
StringBuffer contentURIBuffer = new StringBuffer();
60+
getContentURI(contentURIBuffer, this);
61+
return contentURIBuffer.toString();
62+
}
63+
64+
public String getContentTitle() {
65+
StringBuffer titleBuffer = new StringBuffer();
66+
getContentTitle(titleBuffer, this);
67+
return titleBuffer.toString();
68+
}
69+
70+
private void getContentURI(StringBuffer contentURIBuffer, FocusPoint focusPoint) {
71+
FocusPoint parentFocuPoint = focusPoint.getParentFocusPoint();
72+
73+
if (parentFocuPoint != null) {
74+
getContentURI(contentURIBuffer, parentFocuPoint);
75+
}
76+
contentURIBuffer.append(URI_SEPARATOR);
77+
contentURIBuffer.append(encode(focusPoint.getName()));
78+
}
79+
80+
private String encode(String name) {
81+
try {
82+
return URLEncoder.encode(name, "UTF-8");
83+
} catch (UnsupportedEncodingException e) {
84+
return name;
85+
}
86+
}
87+
88+
private void getContentTitle(StringBuffer titleBuffer, FocusPoint focusPoint) {
89+
FocusPoint parentFocusPoint = focusPoint.getParentFocusPoint();
90+
91+
if (parentFocusPoint != null) {
92+
getContentTitle(titleBuffer, parentFocusPoint);
93+
titleBuffer.append(TITLE_SEPARATOR);
94+
}
95+
titleBuffer.append(encode(focusPoint.getName()));
96+
}
97+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2015 Siddique Hameed
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.analytics;
18+
19+
import java.net.InetAddress;
20+
import java.net.UnknownHostException;
21+
import java.util.Date;
22+
import java.util.Random;
23+
24+
/**
25+
* URL building logic for the earlier versions of google analytics (urchin.js)
26+
*
27+
* @author : Siddique Hameed
28+
* @version : 0.1
29+
*/
30+
31+
public class GoogleAnalytics_v1_URLBuildingStrategy implements URLBuildingStrategy {
32+
private FocusPoint appFocusPoint;
33+
private String googleAnalyticsTrackingCode;
34+
private String refererURL = "http://www.BoxySystems.com";
35+
36+
private static final String TRACKING_URL_Prefix = "http://www.google-analytics.com/__utm.gif";
37+
38+
private static final Random random = new Random();
39+
private static String hostName = "localhost";
40+
41+
static {
42+
try {
43+
hostName = InetAddress.getLocalHost().getHostName();
44+
} catch (UnknownHostException e) {
45+
//ignore this
46+
}
47+
}
48+
49+
50+
public GoogleAnalytics_v1_URLBuildingStrategy(String appName, String googleAnalyticsTrackingCode) {
51+
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
52+
this.appFocusPoint = new FocusPoint(appName);
53+
}
54+
55+
public GoogleAnalytics_v1_URLBuildingStrategy(String appName, String appVersion, String googleAnalyticsTrackingCode) {
56+
this.googleAnalyticsTrackingCode = googleAnalyticsTrackingCode;
57+
this.appFocusPoint = new FocusPoint(appVersion, new FocusPoint(appName));
58+
}
59+
60+
61+
public String buildURL(FocusPoint focusPoint) {
62+
63+
int cookie = random.nextInt();
64+
int randomValue = random.nextInt(2147483647) - 1;
65+
long now = new Date().getTime();
66+
67+
// 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." +
68+
// "'&utmac='.$var_utmac.'" +
69+
// "&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";
70+
71+
72+
focusPoint.setParentTrackPoint(appFocusPoint);
73+
StringBuffer url = new StringBuffer(TRACKING_URL_Prefix);
74+
url.append("?utmwv=1"); //Urchin/Analytics version
75+
url.append("&utmn=" + random.nextInt());
76+
url.append("&utmcs=UTF-8"); //document encoding
77+
url.append("&utmsr=1440x900"); //screen resolution
78+
url.append("&utmsc=32-bit"); //color depth
79+
url.append("&utmul=en-us"); //user language
80+
url.append("&utmje=1"); //java enabled
81+
url.append("&utmfl=9.0%20%20r28"); //flash
82+
url.append("&utmcr=1"); //carriage return
83+
url.append("&utmdt=" + focusPoint.getContentTitle()); //The optimum keyword density //document title
84+
url.append("&utmhn=" + hostName);//document hostname
85+
url.append("&utmr=" + refererURL); //referer URL
86+
url.append("&utmp=" + focusPoint.getContentURI());//document page URL
87+
url.append("&utmac=" + googleAnalyticsTrackingCode);//Google Analytics account
88+
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);
89+
return url.toString();
90+
}
91+
92+
public void setRefererURL(String refererURL) {
93+
this.refererURL = refererURL;
94+
}
95+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2015 Siddique Hameed
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark.analytics;
18+
19+
import java.io.IOException;
20+
import java.net.HttpURLConnection;
21+
import java.net.URL;
22+
23+
/**
24+
* Simple class peforming HTTP Get method on the requested url
25+
*
26+
* @author : Siddique Hameed
27+
* @version : 0.1
28+
*/
29+
30+
public class HTTPGetMethod {
31+
private static final String GET_METHOD_NAME = "GET";
32+
33+
private static final String SUCCESS_MESSAGE = "JGoogleAnalytics: Tracking Successful!";
34+
35+
private LoggingAdapter loggingAdapter = null;
36+
37+
public void setLoggingAdapter(LoggingAdapter loggingAdapter) {
38+
this.loggingAdapter = loggingAdapter;
39+
}
40+
41+
private static String uaName = null; // User Agent name
42+
43+
private static String osString = "Unknown";
44+
45+
HTTPGetMethod() {
46+
// Initialise the static parameters if we need to.
47+
if (uaName == null) {
48+
uaName = "Java/" + System.getProperty("java.version"); // java version info appended
49+
// os string is architecture+osname+version concatenated with _
50+
osString = System.getProperty("os.arch");
51+
if (osString == null || osString.length() < 1) {
52+
osString = "";
53+
} else {
54+
osString += "; ";
55+
osString += System.getProperty("os.name") + " "
56+
+ System.getProperty("os.version");
57+
}
58+
}
59+
}
60+
61+
public void request(String urlString) {
62+
try {
63+
URL url = new URL(urlString);
64+
HttpURLConnection urlConnection = openURLConnection(url);
65+
urlConnection.setInstanceFollowRedirects(true);
66+
urlConnection.setRequestMethod(GET_METHOD_NAME);
67+
urlConnection.setRequestProperty("User-agent", uaName + " ("
68+
+ osString + ")");
69+
70+
urlConnection.connect();
71+
int responseCode = getResponseCode(urlConnection);
72+
if (responseCode != HttpURLConnection.HTTP_OK) {
73+
logError("JGoogleAnalytics: Error tracking, url=" + urlString);
74+
} else {
75+
logMessage(SUCCESS_MESSAGE);
76+
}
77+
} catch (Exception e) {
78+
logError(e.getMessage());
79+
}
80+
}
81+
82+
protected int getResponseCode(HttpURLConnection urlConnection)
83+
throws IOException {
84+
return urlConnection.getResponseCode();
85+
}
86+
87+
private HttpURLConnection openURLConnection(URL url) throws IOException {
88+
return (HttpURLConnection) url.openConnection();
89+
}
90+
91+
private void logMessage(String message) {
92+
if (loggingAdapter != null) {
93+
loggingAdapter.logMessage(message);
94+
}
95+
}
96+
97+
private void logError(String errorMesssage) {
98+
if (loggingAdapter != null) {
99+
loggingAdapter.logError(errorMesssage);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)