tags:

views:

89

answers:

2
+2  Q: 

counting a Widget?

Hello I've recently added a weather widget for people to add to their site. Is basically a php page that people can link to.

My question is can I somehow put a counter or something to count how many people are using my widget?

my widget is located here http://www.site.com/widget/ it's in farsi language.

A: 

You can place tracking code on the PHP page. Possibilities are:

  • Google analytics code

  • Record each time the .php page executes by inserting a row in a database

I'm sure there are others, but those should work.

jonstjohn
+1  A: 

Well, it depends - do you want to know how many people have used your widget in their web site, or how many people have watched web sites that use your widgets? These numbers are completely different.

For the later option, jonstjohn's method would work great: For easy implementation and lots of features I recommend the Google Analytics way - they have really fancy graphs that show a lot of interesting information.

If on the other hand you want to count how many web sites are using your widget then you can do as follows:

  • Create a table in your database with a varchar column. Put a unique index on that column.
  • In your code, read the $_SERVER["HTTP_REFERER"] parameter to get the URL that called your widget.
  • Now you want to strip just the domain part from that URL as a web site would probably put your widget in their template so it will be available in all of their pages. So if you want to count web sites and not pages in web sites, then do something like

    $domainParts = explode("/", $_SERVER["HTTP_REFERER"]);
    $domain = $domainParts[2];

  • Now insert the domain you found out into your table. If this web site has already called your widget once then the insert would fail with a unique constraint error - just ignore the error (for example by using "@" on your insert command, like @mysql_query("INSERT INTO...") ).

To know how many web sites are using your widget, simply count the number of rows in the table.

Guss