Skip to content
This repository has been archived by the owner on Jan 9, 2019. It is now read-only.

Commit

Permalink
Merged KM to reorganised front page
Browse files Browse the repository at this point in the history
  • Loading branch information
naleksi committed Mar 6, 2012
2 parents ffa3bf5 + e6d2716 commit 109f90e
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 18 additions & 2 deletions app/controllers/ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
57 changes: 57 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

5 changes: 5 additions & 0 deletions app/models/idea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 9 additions & 8 deletions app/views/comments/_comment.html.haml
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 109f90e

Please sign in to comment.