Im wondering what affect loading an external page with php has on a sites analytics. If php is loading an external page, and not an actual browser, will the javascript that reports back to google analytics register the page load as a hit?
Any JavaScript within the fetched page will not be run and therefore have no effect on analytics. The reason for this is that the fetched HTML page is never parsed in an actual browser, therefore, no JavaScript is executed.
Curl will not automatically download JavaScript files the HTML refers to. So unless you explicitly download the Google Analytics JavaScript file, Google won't detect the Curl hit.
Google offers a non-JavaScript method of tracking hits. It's intended for mobile sites, but may be repurposable for your needs.
You're misunderstanding how curl/file_get_contents work. They're executed on the server, not on the client browser. As far as Google and any regular user is concerned, they'll see the output of those calls, not the calls themselves.
e.g.
client requests page from server A
server A requests page from server B
server B replies with page data to server A
server A accepts page data from server B
server A sends page data to client
Assuming that all the requests work properly and don't issue any warnings/errors and there's no network glitches between server A and server B, then there is absolutely no way for the client to see exactly what server A's doing. It could be sending a local file. It could be executing a local script and send its output. It could be offshoring the request to a server in India which does the hard work and then simply claims the credit for it, etc...
Now, you CAN get the client to talk to server B directly. You could have server A spit out an HTML page that contains an iframe, image tag, script tag, css file, etc... that points to server B. But that's no longer transparent to the client - you're explicitly telling the client "hey, go over there for this content".
Related
I am building a website that will call a third-party web service. This web service is sometimes slow, however, and clients visiting the webpage aren't familiar with the loader on the browser tab. This results to them clicking, again and again, the search button that calls the web service and it gets never completely sent.
I was thinking of making an overlay page with a CSS loader right in the middle of the page so that it's more than obvious that something's loading and force them to wait. I tried researching how to run a script while an HTTP request is being sent, but didn't get any answers. Any thoughts?
Thanks in advance for your time.
The WiP website is https://b2b.a-link.gr/ (unfortunately a client username and password is required to access it) and the web service website is https://www.soft1.eu/. The web service has a database through which I pull out data to make searches on my website.
I have a php application in which I scrape a website and get all of the links present in the site. While I am running the scraper in a tab of a browser and open the app in the other tab of the same browser, it keeps loading until the other tab processing(running scraper) is complete.
I have tried using ajax in this case i.e. I send the request through ajax post to find the links, but it is not effecting.
Any kind of help and guidance will be appreciated.
That is probably caused by the session lock. If your multiple connections (tabs) require the same session, you can't.
If they could be independent, then you would have to pass a session id in the URL to identify which tab is communicating with the server.
Note that the web server may also have restrictions configured on the number of simultaneous sessions from the same IP.
To give a quick example of my question, consider the JS file provided by Google to capture Analytics data that we paste in our files. How does this JS file get all the data? I want to know how to create such JS files that I can give out to others who can then run it on their own websites and yet, store data on my website server. In short, they only copy the 1-2 lines of JS in their pages and all the intended functionality would run on their website, but the data will be stored on my web server database.
How can I attach the functionality to such a JS file? I mean, how can I tell the JS file whether to collect Analytic data or show a form, etc. I am using PHP for server side processing. I did not find any good information on this so far.
Edit 1:
I wanted to add that the functionality is NOT limited just to analytics. It could be even as simple as showing a contact form that sends email to recipients, etc.
Google Analytics has a client-side javascript file that the site-owner puts a reference to in their web page. When that javascript file runs, it collects information about the current page and then makes a request of Google's server with that information encoded in the request and Google's server records that information in their database. Because ajax calls are subject to the same-origin limitations, Google's request back to their server is actually for a GIF image with the data encoded in the URL.
Here's Google's explanation of how it works: http://code.google.com/apis/analytics/docs/concepts/gaConceptsOverview.html
To create something like this for your clients, you would have to create the appropriate javascript file, host it on your servers, give out the instructions for installing it into their web pages and create the right PHP scripts for recording the information that comes in when the GIF is requested (presumably, you'd have to do some web server configuration to get your PHP scripts to run on a GIF request too).
I think you can find the answer to your question here: How to send data to remote server using Javascript
In short, you'll be able to send data to another domain using JSONP. You can achieve this also with jQuery's, $.getJson method.
By inserting something like
<script src="http://myeviltrackingsite.com/track.js"></script>
the visitor's browser will ask your server for track.js. When asked your server will get a normal HTTP-Header from the visitor and of course his IP. This HTTP-Header contains all information you want like the visitor's language, the kind of browser he uses. To track the visitor's geo location you can use the visitor's IP address and do a reverse IP lookup. there are free geo location databases available.
When using PHP I can use file_get_contents or cURL to get a URL.
jQuery runs on the client
In jQuery there is a function called jQuery.getJSON(). Javascript is run on the client. What server is used for the download of the JSON code of the external URL? What information does the called URL know about? Does it know of the domain? The IP of the client user? It's a client language.
Prefered for many request
To make many requests, is it safer to do this with Javascript than PHP because it runs on the every client instead of one server point?
What server is used for the download of the JSON code of the external URL?
The one that the domain name in the URL passed to that function resolves to.
What information does the called URL know about?
It is an HTTP request, like any other. The usual information will be available.
Does it know of the domain? The IP of the client user?
Of course.
It's a client language.
… making an HTTP request.
To make many requests, is it safer to do this with Javascript than PHP because it runs on the every client instead of one server point?
You control the server. You don't control the client. JavaScript can be disabled. It is safer to make the request from your server.
(For a value of "safe" equal to "Less likely to fail assuming the service you are using doesn't impose rate limiting")
Because of the Same Origin Policy all requests made in JavaScript must go to the domain from which the document was loaded. It's a standard HTTP request, so the server will have the same information it would if a user was just navigating around (including cookies, etc.) From the phrasing of your question it appears you need to make requests to some external site, in which case making those requests from your server which is not subject to such a security policy would likely be best.
In jQuery there is a function called jQuery.getJSON(). Javascript is
run on the client. What server is used for the download of the JSON
code of the external URL? What information does the called URL know
about? Does it know of the domain? The IP of the client user? It's a
client language.
The code that runs your web browser is only on your PC, too, yet it is perfectly capable of retrieving content via the HTTP protocol from a web server, and has done so for several decades.
AJAX requests are no different. jQuery creates an XMLHttpRequest object that performs an HTTP request in a manner uncoupled from the general page context. As far as the server's concerned, it's just an HTTP request like any other.
The text contents of the result you get back happen to be written in JSON format, but the HTTP layer neither knows nor cares about that.
i need to fetch a url with javascript/jquery and not php.
i've read that you could do that if you got a php proxy, but that means that it is still going through php. cause then it's still the ip of the server that is fetching it.
could one fetch the url entirely with only front-end, and thus fetch it with the client's ip?
There exists a Same origin policy for AJAX requests. This prevents Javascript on, say, this site, from making a request to gmail.com (with your cookies), reading your e-mails, and uploading them to the StackOverflow server. Javascript on stackoverflow.com can only make AJAX requests to pages on that domain.
As you can see, this is essential for security. Requests must instead be made by a proxy running on your web server - PHP can be used, but there are other solutions. For example, Ajax Cross Domain is an AJAX library that communicates with a Perl script running on the server to emulate AJAX requests for other domains.
It is also possible to make requests on other domains via a javascript include (script tag), image tag, etc. but in these cases you cannot read the contents of the page.
You cannot do this with an iframe either: scripts cannot see the internals of iframes unless they are on the same domain as the script.
So in short, use a proxy.
The problem is that jQuery would fetch an url with AJAX and AJAX won't operate cross-domain because of the potential security (as per the same-origin policy).
There are however ways to emulate this, if you load the page in an iframe you can retrieve the data by using innerHTML on the iframe. Here's an example script that uses jQuery: http://code.google.com/p/jquery-crossframe/