Permalink
Browse files

Merged KM to reorganised front page

  • Loading branch information...
2 parents ffa3bf5 + e6d2716 commit 109f90e80097ec03825a44654c3ebaa533a5d8e0 @naleksi naleksi committed Mar 6, 2012
@@ -6,6 +6,9 @@ class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
+ KM.identify(current_citizen)
+ KM.push("record", "article read", article_id: @article.id, article_title: @article.title) # TODO use permalink title
+
respond_with @article
end
@@ -7,7 +7,12 @@ class CommentsController < ApplicationController
def create
@comment = @idea.comments.build(params[:comment])
@comment.author = current_citizen
- flash[:notice] = I18n.t("comments.create") if @comment.save
+ if @comment.save
+ flash[:notice] = I18n.t("comments.create")
+ KM.identify(current_citizen)
+ KM.push("record", "comment created", idea_id: @idea.id)
+ end
+
respond_with @comment
end
@@ -5,6 +5,8 @@ class IdeasController < ApplicationController
def index
@ideas = Idea.published.paginate(page: params[:page])
+ KM.identify(current_citizen)
+ KM.push("record", "idea list viewed", page: params[:page] || 0)
respond_with @ideas
end
@@ -19,6 +21,9 @@ def show
@colors = ["#8cc63f", "#a9003f"]
@colors.reverse! if @idea_vote_for_count < @idea_vote_against_count
+ KM.identify(current_citizen)
+ KM.push("record", "idea viewed", idea_id: @idea.id, idea_title: @idea.title) # TODO use permalink title
+
respond_with @idea
end
@@ -31,7 +36,11 @@ def create
@idea = Idea.new(params[:idea])
@idea.author = current_citizen
@idea.state = "idea"
- flash[:notice] = I18n.t("idea.created") if @idea.save
+ if @idea.save
+ flash[:notice] = I18n.t("idea.created")
+ KM.identify(current_citizen)
+ KM.push("record", "idea created", idea_id: @idea.id, idea_title: @idea.title) # TODO use permalink title
+ end
respond_with @idea
end
@@ -42,7 +51,11 @@ def edit
def update
@idea = current_citizen.ideas.find(params[:id])
- flash[:notice] = I18n.t("idea.updated") if @idea.update_attributes(params[:idea])
+ if @idea.update_attributes(params[:idea])
+ flash[:notice] = I18n.t("idea.updated")
+ KM.identify(current_citizen)
+ KM.push("record", "idea edited", idea_id: @idea.id, idea_title: @idea.title) # TODO use permalink title
+ end
respond_with @idea
end
@@ -78,6 +91,9 @@ def vote_flow
@authors = @idea_counts.to_json
+ KM.identify(current_citizen)
+ KM.push("record", "vote flow viewed")
+
render
end
end
@@ -35,5 +35,8 @@ def home
end
@blog_articles = Article.published.where(article_type: 'blog').limit(3).all
+
+ KM.identify(current_citizen)
+ KM.push("record", "front page viewed")
end
end
@@ -29,3 +29,60 @@ def Numeric.format(number, *args)
end
end
+
+
+# KissMetrics helper, that generates KM javascript
+require 'digest/sha1'
+
+class KM
+ $pushes ||= []
+ def KM.push(command, event_name, params=nil)
+ if params
+ $pushes.push [command, event_name, params.to_json]
+ else
+ $pushes.push [command, event_name]
+ end
+ end
+ # if no user id given, create default by logged in user email hash, or "null" if not logged in
+ def KM.identify(current_citizen)
+ if current_citizen
+ identify = Digest::SHA1.new.update(current_citizen.email + api_key).hexdigest
+ else
+ identify = "null"
+ end
+ KM.push("identify", identify)
+ end
+ def KM.api_key
+ ENV['KISSMETRICS_API_KEY'] || "690626c087893eef5f7307868202023d84f79acb"
+ end
+
+ def KM.js
+ if $pushes
+ recs = $pushes.map do |pu|
+ if pu[2]
+ "_kmq.push(['#{pu[0]}', '#{pu[1]}', #{pu[2]}]);"
+ else
+ "_kmq.push(['#{pu[0]}', '#{pu[1]}']);"
+ end
+ end.join("\n")
+ $pushes = []
+ "var _kmq = _kmq || [];
+ function _kms(u){
+ setTimeout(function(){
+ var s = document.createElement('script');
+ var f = document.getElementsByTagName('script')[0];
+ s.type = 'text/javascript';
+ s.async = true;
+ s.src = u;
+ f.parentNode.insertBefore(s, f);
+ }, 1);
+ }
+ _kms('//i.kissmetrics.com/i.js');_kms('//doug1izaerwt3.cloudfront.net/#{KM.api_key}.1.js');
+ #{recs}
+ "
+ else
+ ""
+ end
+ end
+end
+
View
@@ -27,9 +27,14 @@ def self.per_page
def vote(citizen, option)
vote = votes.by(citizen).first
+ KM.identify(citizen)
if vote
vote.update_attribute(:option, option) unless vote.option == option
+ KM.push("record", "voted", {option: option, idea: self.id})
+ KM.push("record", "vote change of mind", {option: option, idea: self.id})
else
+ KM.push("record", "voted", {option: option, idea: self.id})
+ KM.push("record", "first vote on idea", {option: option, idea: self.id})
votes.create(citizen: citizen, option: option)
end
end
@@ -1,11 +1,12 @@
.comment
.avatar.grid_2
%img{ src: comment.author.image, width: 50, height: 50 }
- .info
- .grid_6
- %span.index
- %span.name= comment.author.name
- .grid_8
- %span.separator= "|"
- %span.datetime= finnishTime(comment.created_at)
- .body.grid_14= markdown(comment.body)
+ .grid_14
+ .info
+ .grid_6
+ %span.index
+ %span.name= comment.author.name
+ .grid_7
+ %span.separator= "|"
+ %span.datetime= finnishTime(comment.created_at)
+ .body.grid_14= markdown(comment.body)
@@ -21,6 +21,9 @@
/[if lt IE 9]
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
+ :javascript
+ #{KM.js}
+
%body
%header.container_24.top_row_container
.container_24.top_row

0 comments on commit 109f90e

Please sign in to comment.