mirror of
https://github.com/discourse/discourse.git
synced 2025-03-20 13:06:39 +08:00
FEATURE: Use Beacon API for tracking clicks. (#7483)
This commit is contained in:
parent
9adfd66a8b
commit
4ff1195537
@ -153,6 +153,7 @@ export function ajax() {
|
|||||||
if (
|
if (
|
||||||
args.type &&
|
args.type &&
|
||||||
args.type.toUpperCase() !== "GET" &&
|
args.type.toUpperCase() !== "GET" &&
|
||||||
|
url !== "/clicks/track" &&
|
||||||
!Discourse.Session.currentProp("csrfToken")
|
!Discourse.Session.currentProp("csrfToken")
|
||||||
) {
|
) {
|
||||||
promise = new Ember.RSVP.Promise((resolve, reject) => {
|
promise = new Ember.RSVP.Promise((resolve, reject) => {
|
||||||
|
@ -91,15 +91,25 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const trackPromise = tracking
|
let trackPromise = Ember.RSVP.resolve();
|
||||||
? ajax("/clicks/track", {
|
if (tracking) {
|
||||||
|
if (navigator.sendBeacon) {
|
||||||
|
const data = new FormData();
|
||||||
|
data.append("url", href);
|
||||||
|
data.append("post_id", postId);
|
||||||
|
data.append("topic_id", topicId);
|
||||||
|
navigator.sendBeacon("/clicks/track", data);
|
||||||
|
} else {
|
||||||
|
trackPromise = ajax("/clicks/track", {
|
||||||
|
type: "POST",
|
||||||
data: {
|
data: {
|
||||||
url: href,
|
url: href,
|
||||||
post_id: postId,
|
post_id: postId,
|
||||||
topic_id: topicId
|
topic_id: topicId
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
: Ember.RSVP.resolve();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const isInternal = DiscourseURL.isInternal(href);
|
const isInternal = DiscourseURL.isInternal(href);
|
||||||
const openExternalInNewTab = Discourse.User.currentProp(
|
const openExternalInNewTab = Discourse.User.currentProp(
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
class ClicksController < ApplicationController
|
class ClicksController < ApplicationController
|
||||||
skip_before_action :check_xhr, :preload_json
|
skip_before_action :check_xhr, :preload_json, :verify_authenticity_token
|
||||||
|
|
||||||
def track
|
def track
|
||||||
params.require([:url, :post_id, :topic_id])
|
params.require([:url, :post_id, :topic_id])
|
||||||
|
@ -582,7 +582,7 @@ Discourse::Application.routes.draw do
|
|||||||
|
|
||||||
resources :clicks do
|
resources :clicks do
|
||||||
collection do
|
collection do
|
||||||
get "track"
|
post "track"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ describe ClicksController do
|
|||||||
sign_in(Fabricate(:user))
|
sign_in(Fabricate(:user))
|
||||||
|
|
||||||
expect {
|
expect {
|
||||||
get "/clicks/track", params: { url: url, post_id: post.id, topic_id: post.topic_id }, headers: headers
|
post "/clicks/track", params: { url: url, post_id: post.id, topic_id: post.topic_id }, headers: headers
|
||||||
}.to change { TopicLinkClick.count }.by(1)
|
}.to change { TopicLinkClick.count }.by(1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user