I have a controller "track" in codeigniter where I have many methods. In one of the methods I am just redirecting user to some other URL which comes from url parameter as
track.php
function click_thru()
{
redirect($this->uri->segment(3));
}
Before Redirection I want to call google tracker Javascript code. As I am not loading any view in the above method, how do I call JS before redirect?
You can't, PHP and Javascript don't communicate each other like this, you have two solutions :
Either finding a way to do your redirection in javascript after the google tracker call is called (but you have to load the entire body of the response just for one redirection, so not very cool).
Either you find a PHP API that permits you to interact with Google Analytics. Like this one : https://code.google.com/p/php-ga/
Related
I haven't worked in server side in a while and any help is really appreciated.
I am trying to create a small application that has an index.jsp page. The page has a button which when invoked calls a public SaaS API that does something in an external server with no changes to the view in the browser (still has index.jsp loaded).When the external job is done, my callback servlet is called by the service.
Now I need to update a component of the JSP when the callback servlet is called. So, I am doing a request.forward to the already loaded index.jsp with an attribute which when found in the JSP will invoke a javascript function.
The problem is I am unable to invoke the javascript function at all. I tried document.onload or window.load or document.ready etc. and none of these functions are getting called upon the request.forward to the index.jsp, since it is already loaded.
What is the best solution for this? Any help is really appreciated.
You should model your interactions in such a way that on your button click, send the request to a servlet. From the servlet call the external saas api.
When it completes return the response to the servlet(assuming you want a blocking model). Put the data in the request and forward it to the jsp.
I am building a web application using Google Analytics API, but I am running into some difficulties thinking of an implementation.
I am allowing the user to select their profile from three drop-down menus that are dependent on each other (think country->state->city). I am using AJAX to grab a value from the first drop-down (country), and I am trying to populate the second drop-down from Google Analytics API in an external PHP script.
I am trying to bring the Analytics object ($analytics) over to an external script via a $_SESSION variable from index.php, but I cannot call the $analytics functions I need to populate the second drop-down.
I var_dumped the variable, it initially told me it's an incomplete PHP class, so I serialized it and deserialized between the two classes. However, it still gives me:
Fatal error: Call to a member function listManagementWebproperties() on a non-object
I am wondering if there is a way to do this?
Just to clarify:
In index.php
//create a session analytics object for use in external scripts
$_SESSION['analytics'] = serialize($analytics;) //used for API calls
and the line in an external script throwing the error in my AJAX request, propertyID.php:
$analytics = unserialize($_SESSION['analytics']);
$webProperty = $analytics->management_webproperties->listManagementWebproperties($accountID); //the array used to populate the drop-down
EDIT: Could this be an issue due to the API object?
I suppose it could be related to some transient data that makes the class not serializable.
I think you have to create the Google objects in every page call and serialize in $_SESSION only the data useful to create these instances.
I found a solution/workaround to my problem.
Instead of storing the Google_Service_Analytics() object in the session, you can store the Google_Client() object in the session instead. From there you can declare the $analytics object as you would normally:
$analytics = new Google_Service_Analytics($client);
I need a simple way to call a URL to send a command to the system without processing or displaying whatever result is returned. If I use simple HTML calls then the frame the page with the button is on changes to the result returned by my lighting system and my page of buttons goes away. If I use ajax then I run into cross domain issues.
At it's simplest I would just like to have a button process an onclick() and execute a URL (e.g. http://www.mydomain.com/lightingdevice/on) in the background or some simple javascript that runs this same URL while the frame continues to display the page with the button.
Either way you need to use AJAX if you need to do this discretely.
The interface is on the same domain as your application: just use AJAX properly and you're good.
The interface is on some another domain as your application: create an interface on the same domain as your application, using PHP for example. This will let you use AJAX. Then just let PHP handle all the rest.
I saw here similar questions but I didn't find any answer about Javascript.
I'm building a web site (lets call it 'A'), and I want to get content from another web site ('B') that requires a user-name and password.
I want a function at my site that get the content from a certain page at B. I'm always login manually to site B at my computer so I don't need the function to do the login (so I link it directly to the correct page of the info at B).
A and B are not in the same domain. Is there a way in Javascript to get content from B?
You need a cross-doman AJAX call (normally prevented by the same origin policy). jQuery has a handy helper function for this that will return JSON data called $.getJson()
$.getJSON('http://otherdomain/ajax/test.json', function(data) {
if (undefined != data) {
console.log(data);
}
});
This exploits a technique known as JSONP, which writes Javascript directly into the document to make the request (instead of using the XMLHttpRequest object), bypassing the same origin policy.
What I like to do is use YQL (Yahoo Query Language)
It's like an api for api's. I get whatever html I want using selectors and process that.
For example, I can grab all the images from this wikipedia link using a query like
SELECT * FROM html WHERE url="http://en.wikipedia.org/wiki/List_of_United_States_National_Parks_by_state" AND xpath="//img" and then processing the returned XML/JSON
you can test queries HERE
and see an example of grabbing and processing the images in this Fiddle
No there is no way to get content from external B page using pure javascript, but you can try to use php curl or file_get_contents
After a user fills in my "new" user form on "example-one.com", the "create" controller creates the record in the db. Then it does a redirect_to to an external site "payment-checkout.com". I have setup the Google Analytics code on both sites.
Google provides two functions _link and _linkByPost for use to use in any links or forms that go to your external domains. The problem is the user is being redirected by the controller action outside of the view and I cant use those two javascript functions to pass on the relevent G.A. info - what do i do?
Can anyone help?
The way _link works is by passing the Google Analytics cookies from your first domain via a query string to your second domain. The second domain, if configured correctly, will accept those URL parameters and apply them as cookie values for the purposes of tracking.
So, it shouldn't be difficult for you to apply your own version of the _link function.
Specifically, the _link function passes the following cookies:
__utma, __utmb, __utmc, __utmx, __utmz, __utmv and __utmk
Into a query string as such: ?__utma=87278922.614105561.1288923931.1294376393.1298325957.6&__utmb=87278922.1.10.1298325957&__utmc=87278922&__utmx=-&__utmz=87278922.1288923931.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)&__utmv=-&__utmk=72493274
So, all you need to do to replicate the _link function is, before you apply the server side redirect, grab the cookie values, and apply them as a query string on the URL you're redirecting to.
Now, that's not the only thing you'll need to do to get this working. The Google Analytics configuration on the payment site will need to be configured with _setAllowLinker set to true, as well as potentially disabling the domain hash and setting a particular domain name for the tracking cookies; it depends on your configuration. You can find out more about that in Google Analytics Cross Domain Tracking Guide.
#yc's approach looks like the best bet but if that doesn't work, I would suggest having your controller redirect the user to a "temp" page on your site itself and show some text like "Checking out....Please wait..." and using Javascript trigger the call to the "_link" function to redirect the user to the "payment-checkout.com" (again using Javascript).
I assume you're also tracking the page the user returns to and want to measure how many users you lose in the process in between?
My knowledge of the Google Analytics API is fairly limited, so maybe there's a better solution, but you could consider rendering a page containing the GA code and triggering the _link() function from there?
It might also be possible to perform an AJAX call on submitting the form (maybe using remote_form_for) and handling the GA redirect in an RJS-response:
page << "_gaq.push(['_link', 'http://example.com/test.html']);"
However, I'm not sure how well that would fit into your application.