0
204ピクセルのno_content応答を使用するようにトラッキングピクセルJS機能を変換しようと数日間試しています。204応答&コールバック機能付きビーコントラッキングイメージ
私はこの作業を簡単に行うことができますが、後でコールバック機能を起動する必要があります。
204が返されると、次のことは起こっていないようです。
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
error: function() {
console.log('error');
},
success: function() {
console.log('success');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
if (beacon.onerror) {
beacon.onerror = opts.error;
}
if (beacon.onload) {
beacon.onload = opts.success;
}
$(beacon).on('load', function(response, status, xhr){
alert(status);
});
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
トラッキングは正しく記録されますが、アラートやコンソールログメッセージは記録されません。これは可能ですか、私はただ時間を無駄にしていますか?
編集------ここに下記のソリューションに基づいて
は最終版(これはエラー&成功の両方が同じコールバックを使用することを前提としています。
beacon: function (opts) {
var beacon = new Image();
opts = $.extend(true, {}, {
url: pum_vars.ajaxurl || null,
data: {
action: 'pum_analytics',
_cache: (+(new Date()))
},
callback: function() {
console.log('tracked');
}
}, opts);
// Create a beacon if a url is provided
if (opts.url) {
// Attach the event handlers to the image object
$(beacon).on('error success done', opts.callback);
// Attach the src for the script call
beacon.src = opts.url + '?' + $.param(opts.data);
}
}
これまで、私は試したことがありました(「エラー」)。今すぐ完璧に動作します。 Doh !. –
if(!beacon.onerror)をif( "onerror" in beacon)と同じように変更しますか? –
他の誰かのために、!beacon.onerrorもうまくいきます。迅速な解決策のために@Buskyに感謝します。 –