Added reset and debug

Andrew Kane committed Apr 23, 2014
commit ced33400d884f9639a155ce73227f2b58a258fa1
Showing 3 changed files with 72 additions and 35 deletions
CHANGELOG.md +7 -0
@@ @@ -1,3 +1,10 @@
+ ## 0.1.3 [unreleased]
+
+ - Added `ahoy.reset()`
+ - Added `ahoy.debug()`
+ - Added experimental support for native apps
+ - Prefer `ahoy` over `Ahoy`
+
## 0.1.2
- Attach user on Devise sign up
README.md +28 -10
@@ @@ -113,15 +113,35 @@ Ahoy uses [Geocoder](https://github.com/alexreisner/geocoder) for IP-based geoco
### Multiple Subdomains
- To track visits across multiple subdomains, add this to your layout **before** the javascript files.
+ To track visits across multiple subdomains, add this **before** the javascript files.
- ```html
- <script>
- var Ahoy = {"domain": "yourdomain.com"};
- </script>
+ ```javascript
+ var ahoy = {"domain": "yourdomain.com"};
+ ```
+
+ ### Development [master]
+
+ Ahoy is built with developers in mind. You can run the following code in your browser’s console.
+
+ Force a new visit
+
+ ```javascript
+ ahoy.reset(); // then reload the page
+ ```
+
+ Log messages
+
+ ```javascript
+ ahoy.debug();
+ ```
+
+ Turn off logging
+
+ ```javascript
+ ahoy.debug(false);
```
- ### Native Apps [master]
+ ### Native Apps [experimental] [master]
When a user launches the app, create a visit. Send a `POST` request to `/ahoy/visits` with:
@@ @@ -183,10 +203,8 @@ Ahoy.visit_model = UserVisit
Change the platform on the web
- ```html
- <script>
- var Ahoy = {"platform": "Mobile Web"}
- </script>
+ ```javascript
+ var ahoy = {"platform": "Mobile Web"}
```
## TODO
vendor/assets/javascripts/ahoy.js +37 -25
@@ @@ -3,24 +3,16 @@
(function (window) {
"use strict";
- var debugMode = true;
- var options = window.Ahoy || {};
+ var ahoy = window.ahoy || window.Ahoy || {};
var $ = window.jQuery || window.Zepto || window.$;
var visitToken, visitorToken;
- var visitTtl, visitorTtl;
-
- if (debugMode) {
- visitTtl = 0.2;
- visitorTtl = 5; // 5 minutes
- } else {
- visitTtl = 4 * 60; // 4 hours
- visitorTtl = 2 * 365 * 24 * 60; // 2 years
- }
+ var visitTtl = 4 * 60; // 4 hours
+ var visitorTtl = 2 * 365 * 24 * 60; // 2 years
// cookies
// http://www.quirksmode.org/js/cookies.html
- function setCookie(name, value, ttl, domain) {
+ function setCookie(name, value, ttl) {
var expires = "";
var cookieDomain = "";
if (ttl) {
@@ @@ -28,8 +20,8 @@
date.setTime(date.getTime() + (ttl * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
- if (domain) {
- cookieDomain = "; domain=" + domain;
+ if (ahoy.domain) {
+ cookieDomain = "; domain=" + ahoy.domain;
}
document.cookie = name + "=" + value + expires + cookieDomain + "; path=/";
}
@@ @@ -50,9 +42,13 @@
return null;
}
- function debug(message) {
- if (debugMode) {
- window.console.log(message, visitToken, visitorToken);
+ function destroyCookie(name) {
+ setCookie(name, "", -1);
+ }
+
+ function log(message) {
+ if (getCookie("ahoy_debug")) {
+ window.console.log(message);
}
}
@@ @@ -63,16 +59,16 @@
if (visitToken && visitorToken && visitToken != "test") {
// TODO keep visit alive?
- debug("Active visit");
+ log("Active visit");
} else {
- setCookie("ahoy_visit", "test", 1, options.domain);
+ setCookie("ahoy_visit", "test", 1);
// make sure cookies are enabled
if (getCookie("ahoy_visit")) {
- debug("Visit started");
+ log("Visit started");
var data = {
- platform: options.platform || "Web",
+ platform: ahoy.platform || "Web",
landing_page: window.location.href
};
@@ @@ -85,15 +81,31 @@
data.visitor_token = visitorToken;
}
- debug(data);
+ log(data);
$.post("/ahoy/visits", data, function(response) {
- setCookie("ahoy_visit", response.visit_token, visitTtl, options.domain);
- setCookie("ahoy_visitor", response.visitor_token, visitorTtl, options.domain);
+ setCookie("ahoy_visit", response.visit_token, visitTtl);
+ setCookie("ahoy_visitor", response.visitor_token, visitorTtl);
}, "json");
} else {
- debug("Cookies disabled");
+ log("Cookies disabled");
}
}
+ ahoy.reset = function () {
+ destroyCookie("ahoy_visit");
+ destroyCookie("ahoy_visitor");
+ return true;
+ };
+
+ ahoy.debug = function (enabled) {
+ if (enabled === false) {
+ destroyCookie("ahoy_debug");
+ } else {
+ setCookie("ahoy_debug", "t", 365 * 24 * 60); // 1 year
+ }
+ return true;
+ };
+
+ window.ahoy = ahoy;
}(window));