Permalink
Browse files

added an event model to store logged in user events, these will be us…

…ed for a timeline feature
  • Loading branch information...
1 parent f51341b commit d6b05183fa54d31ee2696e76093d310b15da53ea @cjstewart88 committed Jan 8, 2013
@@ -10,4 +10,5 @@
//= require import
//= require tabs
//= require follow
+//= require report
//= require custom-playlist-settings
@@ -12,7 +12,7 @@ var Player = {
// lets keep the session active in google analytics to
// track better avg visit duration
setInterval(function () {
- _gaq.push(['_trackEvent', 'ping', 'pong']);
+ Report.gaPing();
}, 60000);
},
@@ -40,8 +40,13 @@ var Player = {
onPlayerStateChange: function (newState) {
if (newState.data == 0) {
- // track full video views
- _gaq.push(['_trackEvent', 'video', 'watched in full', Playlist.videos[Playlist.currentTrack].videoID]);
+ Report.gaVideoView();
+ Report.event({
+ event: 'watchedVideo',
+ video_id: Playlist.videos[Playlist.currentTrack].videoID,
+ video_title: Playlist.videos[Playlist.currentTrack].videoTitle
+ });
+
Playlist.nextSong();
}
@@ -60,22 +60,13 @@ var Playlist = {
},
report: function () {
- var url;
-
- if (Playlist.options.searchType == 'customPlaylist') {
- url = [Playlist.options.customPlaylistOwner.replace(/[ ]/g,"+"), "playlist", Playlist.options.customPlaylistName.replace(/[ ]/g,"+")];
- }
- else if (Playlist.options.searchType == 'video') {
- url = [Playlist.options.searchType, Playlist.options.videoID];
- }
- else if (Playlist.options.searchType == 'reddit') {
- url = ['r', Playlist.options.subReddit];
- }
- else {
- url = [Playlist.options.searchType, Playlist.options.search.replace(/[ ]/g,"+")];
- }
-
- _gaq.push(['_trackPageview', url.join('/')]);
+ Report.gaPageview();
+ Report.event({
+ event: Playlist.options.searchType,
+ query: Playlist.options.search || Playlist.options.subReddit || null,
+ playlist_name: Playlist.options.customPlaylistName,
+ playlist_owner: Playlist.options.customPlaylistOwner
+ });
},
determineIfSpecialSearch: function () {
@@ -0,0 +1,36 @@
+var Report = {
+
+ gaPageview: function () {
+ var url;
+
+ if (Playlist.options.searchType == 'customPlaylist') {
+ url = [Playlist.options.customPlaylistOwner.replace(/[ ]/g,"+"), "playlist", Playlist.options.customPlaylistName.replace(/[ ]/g,"+")];
+ }
+ else if (Playlist.options.searchType == 'video') {
+ url = [Playlist.options.searchType, Playlist.options.videoID];
+ }
+ else if (Playlist.options.searchType == 'reddit') {
+ url = ['r', Playlist.options.subReddit];
+ }
+ else {
+ url = [Playlist.options.searchType, Playlist.options.search.replace(/[ ]/g,"+")];
+ }
+
+ _gaq.push(['_trackPageview', url.join('/')]);
+ },
+
+ gaVideoView: function () {
+ _gaq.push(['_trackEvent', 'video', 'watched in full', Playlist.videos[Playlist.currentTrack].videoID]);
+ },
+
+ gaPing: function () {
+ _gaq.push(['_trackEvent', 'ping', 'pong']);
+ },
+
+ event: function (params) {
+ if (User.id) {
+ $.post('/event', { event: JSON.stringify(params) });
+ }
+ }
+
+}
@@ -1,18 +0,0 @@
-class ApiController < ActionController::Base
- def user_playlist
- response = []
- user = User.where(:username => params[:username]).first
-
- if user.present?
- playlist_data = user.playlists.where("lower(playlist_name) = ?", params[:playlist_name].downcase).first
-
- if playlist_data.present?
- playlist_data.videos.each do | video |
- response.push(:videoID => video["video_id"], :videoTitle => video["video_title"])
- end
- end
- end
-
- render :json => response
- end
-end
@@ -0,0 +1,15 @@
+class EventsController < ApplicationController
+ before_filter :validate_current_user
+
+ def create
+ event = JSON.parse(params[:event])
+ current_user.events.create(event)
+ head 201
+ end
+
+ private
+
+ def validate_current_user
+ head 403 and return unless user_signed_in?
+ end
+end
@@ -96,4 +96,21 @@ def update
render :json => to_return
end
+
+ def get
+ response = []
+ user = User.where(:username => params[:username]).first
+
+ if user.present?
+ playlist_data = user.playlists.where("lower(playlist_name) = ?", params[:playlist_name].downcase).first
+
+ if playlist_data.present?
+ playlist_data.videos.each do | video |
+ response.push(:videoID => video["video_id"], :videoTitle => video["video_title"])
+ end
+ end
+ end
+
+ render :json => response
+ end
end
View
@@ -0,0 +1,5 @@
+class Event < ActiveRecord::Base
+ belongs_to :user
+
+ attr_accessible :event, :query, :playlist_name, :playlist_owner, :video_id, :video_title
+end
View
@@ -1,2 +0,0 @@
-class Searches < ActiveRecord::Base
-end
View
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
+ has_many :events
has_many :playlists, :order => "lower(playlist_name) ASC"
has_many :banned_videos
View
@@ -1,2 +0,0 @@
-class VideoView < ActiveRecord::Base
-end
View
@@ -5,8 +5,6 @@
get "/support" => "application#support"
- get "/:username/playlist/:playlist_name.json" => "api#user_playlist"
-
get "/stats" => "application#stats"
get "/just/:artist_band" => "application#index"
get "/similar/:artist_band" => "application#index"
@@ -23,6 +21,7 @@
get "/:username/profile" => "users#profile"
get "/:username/playlists" => "users#profile"
+ get "/:username/playlist/:playlist_name.json" => "playlists#get"
get "/:username/playlist/:playlist_name" => "playlists#listen"
post "/:username/playlist/:playlist_name/sort" => "playlists#sort"
match "/playlist/create" => "playlists#create"
@@ -36,6 +35,8 @@
post "/check_banned" => "bannedVideos#check"
post "/ban_video" => "bannedVideos#ban_video"
+ post "/event" => "events#create"
+
devise_scope :user do
get "/users/sign_out" => "devise/sessions#destroy"
end
@@ -0,0 +1,15 @@
+class CreateEvents < ActiveRecord::Migration
+ def change
+ create_table :events do |t|
+ t.integer :user_id
+ t.string :event
+ t.string :query
+ t.string :playlist_name
+ t.string :playlist_owner
+ t.string :video_id
+ t.string :video_title
+
+ t.timestamps
+ end
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130106172855) do
+ActiveRecord::Schema.define(:version => 20130108043259) do
create_table "banned_videos", :force => true do |t|
t.integer "user_id"
@@ -20,6 +20,18 @@
t.datetime "updated_at", :null => false
end
+ create_table "events", :force => true do |t|
+ t.integer "user_id"
+ t.string "event"
+ t.string "query"
+ t.string "playlist_name"
+ t.string "playlist_owner"
+ t.string "video_id"
+ t.string "video_title"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
create_table "follows", :force => true do |t|
t.integer "followable_id", :null => false
t.string "followable_type", :null => false
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Event do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit d6b0518

Please sign in to comment.