I am using javascript to access the GA cookie __utmz when a user hits the page. The problem I am having I am unable to get the information on the first page view. The user must either refresh or navigate to a second page before I can see the information.
Is it possible to grab this info on first page view. If there is specific parts of my code you would like to see please let me know.
You can only access __utmz after the callback from Google has completed. As long as you're using a newer (~1.6+?) version of jQuery, the following should work:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
$.ajax({
url: ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js',
dataType: "script",
cache: true,
success: function() {
// Cookie reading code goes here
console.log(readCookie("__utmz"));
}
});
Related
I'm converting a site from ColdFusion to Lucee (for the first time). In ColdFusion, after using the cfajaximport tag, I can run JS code similar to this:
ColdFusion.Ajax.submitForm('runMe', 'runCode.cfm', callback_testMe, errorHandler_testMe);
I seem to be unable to run this in Lucee. I'm looking for some kind of Lucee alternative, but can't seem to find anything.
Basically, I'm wanting to submit form data, run some server side stuff, then return the results without refreshing the page.
Any help would be greatly appreciated.
Lucee does not have CF UI tags, you're going to have to do this with jQuery AJAX or similar.
var formID = "runMe"; // formId Param
$("#"+formID).on("submit", function (e) {
e.preventDefault(); // Prevent page load
$.ajax({
type: "POST", // httpMethod Param
url: "runCode.cfm", // URL Param
data: $(this).serialize(),
success: function (data) {
console.log(data); // callbackhandler
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(thrownError); // errorHandler
}
});
})
This should do exactly the same thing and I even pointed out the similar params from ColdFusion.Ajax.submitForm
I am using Devise gem for authentication. I have used timeoutable feature of devise to redirect to login page if no user activity seen for specified time. The issue is mainly when ajax request is sent and session timed out, the UI becomes nonresponsive. Below is my configuration:
In User model:
devise :timeoutable
def timeout_in
if self.role.name == "Admin"
30.minutes
end
end
This does the timeout(session expires after 30 mins if no user activity is seen). But it does not autorefresh page to show the signin page. I have even tried to use javascript that does the regular check if session has expired or not and if expired, just reload the page mainly for ajax request in order to avoid unresponsive UI. But this does not work either. Any help would be appreciated..
Whenever AJAX request encounters a 401 i.e., Unauthorized token error, refresh/ reload the page :
var jqXHR = $.ajax({
url: "/validate_barcode",
type: "GET",
dataType: "json",
data: {barcode: barcode},
}).done(function (data,status,jqXHR) {
console.log("Success");
}).fail(function (jqXHR,status,err) {
// alert("Promise error callback.");
if(jqXHR.status == 401){
location.reload();
}
})
This worked and reloaded the page to show login screen if session timedout and UI became non responsive.
var message = 'test';
var picture = 'http://l.yimg.com/f/i/tw/ks/show/120604_mntl01.jpg';
var link = 'https://www.youtube.com/watch?v=BIl8Px1ds3c';
var name = 'great';
var description = 'des';
FB.api('/1437247769881131/feed', 'post', {message: message, picture: picture, name: name, description: description },function (response){
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
I tried posting a test message to my fan page wall , but it become a visitor post(on left)
How could i post to the wall , if i change my admin to fan page admin ,it pop up a dialog
[You're using Facebook as a Page]
[To continue, you'll need to switch from using Facebook as 談政宏 to using Facebook as 談政宏.]
So i change back to person admin , but i can only post a visitor post
How can I post as Page Admin ?
You just need to use a Page Token to post "as Page". Right now you are using a User Token so it will get posted "as User". Information about Access Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
Hi im using an unoffical fb Coldfusion SDK i had to convert it all from using CFscript to conventional cffunctions because im still on CF7.
Everything seems to be working but im not sure why when trying to lookup the cookie on login at server side its not found. Its seems to be fine on Chorme but FF, IE and Opera all have the same issue.
On index.cfm i have a fb:login button, when pressed you get the log in screen, once successfully logged in, index.cfm refreshs runs my FacebookApp methods but returns a blank value for access_token when read from server side. This is because at server side i try to get the info from the cookie but the cookie has not been created. Ive also outputted the value of access_token but it returns as blank. When i press F5 about a second later I now see the value for my access_token in my output. At the same time if I use the js alert to display access_token in getLoginStatus i can see the value.
Ive read in some places for older browsers where the SDK is slow to load to use the channelURL param, which ive done but im still getting the same result as above.
Any suggestions what I can do? I tried adding js timeout to slow down the to getLoginStatus so it had time to read the cookie but ive not had any joy. Please help.
Top of the page i have this
<!doctype html PUBLIC "-//W3C//Dtd html 4.0 Transitional//EN">
<html xmlns:og="http://ogp.me/ns#" xmlns:fb="http://www.facebook.com/2008/fbml">
After the body tag i have the following
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : 'xxxxxxxxxxxxxxxx',
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // OAuth 2.0
status : true, // check login status
xfbml : true, // parse XFBML
channelUrl: document.location.protocol + './/www.sitename.com/fbchannel.html' //custom channel
});
// whenever the user logs in or logs out, we refresh the page
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
alert('login');
});
FB.Event.subscribe('auth.logout', function(response) {
window.location.reload();
//alert('logout');
});
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
// the user is logged in and connected to your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
//alert('end');
// alert('login url called '+unescape(window.location.pathname));
// whenever the user logs in we refresh the page
//alert(accessToken);
} else if (response.status == 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
} else {
// the user isn't even logged in to Facebook.
}
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
Somewhere way below the stuff above i have my button
<fb:login-button scope="publish_stream,email" autologoutlink="true">Connect with FB</fb:login-button>
After spending DAYS on this trying to figure out the problem i came across this and its made my day, problem solved! Thanks chwk.
I have seen this question in SO several times. Most of cases user called facebook js (all.js) twice or did not provide the application id. But my case is different. I have included all.js once and I also have set application id. But still I am getting this error "FB.login() called before calling FB.init()" when I click the login button. The markup for login button is as follows
<fb:login-button autologoutlink="true" perms="email,user_birthday,user_about_me,publish_stream"></fb:login-button>
And the javascript codes are
<script type="text/javascript">
FB.init({appId: '${section.parameters['facebook.app.id']}', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
//logout();
});
function setFacebookStatus(form) {
var status = $('#comment-form-body').val();
var params = {};
params['message']= status;
params['link'] = '${article.url}';
FB.api('/me/feed', 'post',params, function(response) {
if (!response || response.error) {
alert('Error occured');
}
else {
alert('<fmt:message key="comment.form.facebook.success" />');
$("#facebookComment").overlay().close();
form.submit();
}
});
}
</script>
I have added all.js in the header of my html page. I am really pissed off this issue. I would be very grateful if you help me.
Edit
One more thing to mention. Sometimes it works and sometimes it shows me the warning. Say I was logged in to facebook from another tab and then I logged out from facebook. Then I refresh my website page that has the facebook integration. Then I get the warning. Main problem is that sometimes it is working sometimes not.
I have modified my javascript as follows and now it is working.
$(function() {
window.fbAsyncInit = function() {
FB.init({appId: '${section.parameters['facebook.app.id']}', status: true, cookie: true, xfbml: true});
};
$('body').append('<div id="fb-root"></div>');
$.getScript(document.location.protocol + '//connect.facebook.net/en_US/all.js');
})
setFacebookStatus function remain same. Adding those scripts on document ready removes the problem of facebook javascript warning and non deterministic login behavior. My guess is that on document ready initialization of FB scripts ensures execution of all calls in required order.