Copy link to clipboard
Copied
Hello;
I am working on a small script that checks for the users resolution. I know the only way to do this is by a java script sending the info to coldfusion then adding it to a db. So I wrote this script and it grabs the users resolution, but once a size is put into the db, all the other users that hit the site, are using the same resolution. I find that hard to believe.
This is my code:
These tags are set in my application.cfc file in onsessionstart
<!--- This grabs the users resolution --->
<CFSET ScreenWidth = "<script>document.write(screen.width);</script>">
<CFSET ScreenHeight = "<script>document.write(screen.height);</script>">
<!--- then add it to the DB --->
<cfquery name="tracking" datasource="#APPLICATION.dataSource#" dbtype="ODBC">
INSERT INTO tracking ( REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO )
VALUES(
<cfqueryparam value="#Trim(CGI.REMOTE_ADDR)#" cfsqltype="CF_SQL_VARCHAR">,
<cfif Len(Trim(HTTP_USER_AGENT)) GT 1>
<cfqueryparam value="#Trim(CGI.HTTP_USER_AGENT)#" cfsqltype="CF_SQL_VARCHAR">,
</cfif>
<cfqueryparam value="#Trim(ScreenWidth)#" cfsqltype="CF_SQL_VARCHAR">,
<cfqueryparam value="#Trim(ScreenHeight)#" cfsqltype="CF_SQL_VARCHAR">,
<cfqueryparam value="#Now()#" cfsqltype="CF_SQL_TIMESTAMP">,
<cfqueryparam value="#Trim(PATH_INFO)#" cfsqltype="CF_SQL_LONGVARCHAR">
)
</cfquery>
then to read it I use this:
<cfquery NAME="tracking" datasource="#APPLICATION.dataSource#">
SELECT ID, REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO
FROM tracking
ORDER BY ID
</cfquery>
<cfoutput query="tracking">
#SWidth# x #SHight#
</cfoutput>
That is it. Anyone have any idea what I did wrong on this? Even a better more simple way would be good.
thank you.
CFmonger
Copy link to clipboard
Copied
You need a WHERE clause in your tracking query otherwise you will always get the latest result
Michael
Copy link to clipboard
Copied
how would I write a where statement for this?
Copy link to clipboard
Copied
Use the other fields to identify which user this is:
<cfquery NAME="tracking" datasource="#APPLICATION.dataSource#">
SELECT ID, REMOTE_ADDR, HTTP_USER_AGENT, SWidth, SHight, TRACK_DATE, PATH_INFO
FROM tracking
WHERE REMOTE_ADDR = '#Trim(CGI.REMOTE_ADDR)#'
AND HTTP_USER_AGENT = '#Trim(CGI.HTTP_USER_AGENT)#'
ORDER BY ID
</cfquery>
Alternatively you may be able to make use of session variables or cookies
Copy link to clipboard
Copied
That didn't work. Wouldn't I need to do something with the insert, to make sure the proper user had the proper info added to the db table? The query you attached the where statement to is just to view the records that were added by the insert function in the application.cfc
Copy link to clipboard
Copied
If your users are logged in then you can use their user names - although you'd still need to check in case of multiple log ins
What are you trying to do with the values? If the App.cfm file is reading the values each time then they can be read from there avoiding the database
Copy link to clipboard
Copied
If you don't know how to write where statements, I have heard good things about the book Teach Yourself SQL in 10 Minutes by Ben Forta.
Having said that, you have bigger issues because your approach will result in duplicate db records, always a bad thing.
Copy link to clipboard
Copied
So your saying I should have an insert query, and an update query to update existing records if a user comes back. I would need to set a coockie to do that and check to see if the cookie exists to see if they had been to the site before. Correct?
yes, I am weak on writting sql still, been working on it, I will look for that book.
In the mean time, how would I get the insert to do the right thing as this sits right now. I can add the cookie fucntion in later if that is the best approach for this. I was working on that feature anyway.
thanks
CFmonger
Copy link to clipboard
Copied
You are starting to find the right track.
With computer programming, there is often more than one way to do things. To keep track of site visits, cookies are one way, but you relinquish control of your data because people can clear their cookies. I'd use a database entry myself.
You are also on the right track by wanting to update existing records with the latest data. This sort of logic will help you with that.
First run query to see if you have a record.
if so
update it
else
insert
Copy link to clipboard
Copied
I like that idea better, I was also going to add like a "hit counter" so if it is updating the record, it adds +1 to a cell in the db so if they had 5 sessions, it would show 2 sessions and so on.
Right now, this is just the start of all that, I wanted to make sure I was collecting the info properly before I started playing with it. Don't know if that is the proper way to do things, but I like that approach. get the core built, then add on to it.
do you have any idea why my cfset is wrtting the actual script in my db and not the results from the cfset script?
I am making this more for marketing reasons, I know there are a lot out there, but, I kind of like making my own, I can make them do what I want and not have to figure out someone elses code to recode it. I learn more this way also.
thanks again
CFmonger
Copy link to clipboard
Copied
If I was counting hits, I would have that in a separate table with one record per page request. More flexibility that way.
Copy link to clipboard
Copied
Do you need the info in the database?
what happens if you change the display page to just:
<cfoutput>#ScreenWidth# x #ScreenHeight#</cfoutput>
If this doesn't work then you may need to adjust the scope - hopefully someone else can correct any mistakes I make here - try either:
<cfoutput>#application.ScreenWidth# x #application.ScreenHeight#</cfoutput>
or
change the app.cfm file :
<!--- This grabs the users resolution --->
<CFSET session.ScreenWidth = "<script>document.write(screen.width);</script>">
<CFSET session.ScreenHeight = "<script>document.write(screen.height);</script>">
and the display page:
<cfoutput>#session.ScreenWidth# x #session.ScreenHeight#</cfoutput>
Copy link to clipboard
Copied
Yes, I am making a small tracking system, and I need to collect info on screen resolution. I did try the session.screenwidth and so on thinking it would narrow down the results by the actual session that is started. No, that didn't work either.I really think it has to do with what is being collected by the cfset and getting inserted into the DB is where the problem is.
so far those didn't work, and some I tried.BUT I did find this.
In my DB the actual resolution is not being collected, but this is being added to my db = <script>document.write(screen.width);</script>
so the cfset is not collecting the proper info. Any ideas?
CFmonger