I have universal analytics installed on my website, and want to parse the __utmz cookie to get referral info. However, I never see this cookie set.
Has something changed? Any reason this isn't set?
I do see the _ga cookie when I browse my site, and I see the __utmz cookie in my browser cache if I go to other sites.
I checked out the docs, and don't see any reference to this changing recently, so a bit stumped.
Universal Analytics doesn't create any __utm* cookies.
However, you can use Universal Analytics code (analytics.js) AND the traditional code (ga.js) simultaneously on your site. This will allow you to populate your UA profile and scrape the values from __utmz.
It seems like with Universal Analytics, this cookie has disappeared, and you only get a single _ga cookie.
Source: https://developers.google.com/analytics/devguides/collection/analyticsjs/cookie-usage
Also mentioned here: How to get the referrer, paid/natural and keywords for the current visitor in PHP with new Google Analytics?
Also given that analytics is primarily a tool to collect aggregated information, I couldn't find (and I doubt) that there is any way to query GA to get this info back, given the _ga cookie.
You can create your own cookie and store the query string parameters that google analytics use (utm_campaign and etc).
See this project as example:
https://github.com/dm-guy/utm-alternative
Use below code to get utmz cookie along with your universal analytics js code
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Related
I am trying to get a Facebook page feed through Google app script.
As of now I tried different scripts but I am getting only app token with the request and if i change it with a usertoken from graph api I got messages but no images and titles
How to get the user token and get the correct fields for as a json ,
var url = 'https://graph.facebook.com'
+ '/love.to.traavel/feed'
+ '?access_token='+ encodeURIComponent(getToken());
// + '?access_token=' + service.getAccessToken();
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var jsondata = JSON.parse(json);
Logger.log(jsondata); //check this and adjust following for loop and ht
var posts = {};
for (var i in jsondata) {
posts[i] = {"post":jsondata[i].message};
}
return posts;
You should use a Page Token, not a User Token
You need to ask for fields you want to get, with the fields parameter: https://graph.facebook.com/love.to.traavel/feed?fields=field1,field2,...&access_token=xxx
You get a user token by authorizing your App: https://developers.facebook.com/docs/facebook-login/platforms
Be aware that extended user tokens are valid for 60 days only, so you have to refresh it once in a while. There is no user token that is valid forever. You cannot authorize through code only, it needs user interaction. The easiest way is to just generate a user token by selecting your App in the API Explorer and authorize it, like you did already. Then hardcode it in the script code.
Alternatively, you can try implementing this with the manual login flow, check out the docs for that. You can try adding the functionality using this for a custom interface where you go through the login process: https://developers.google.com/apps-script/guides/html/
Since you donĀ“t own the page, you should read this too: https://developers.facebook.com/docs/apps/review/feature/#reference-PAGES_ACCESS
I would like to connect to the Facebook Ads Insights API with google scripts in order to generate and update a google sheet containing my ads key performance indicators.
I have read Facebook's documentation but I'm a bit lost, for example In the documentation's website, I can see that I am supposed to follow this syntax to get a campaign's impressions
GET <AD_OBJECT>/insights?fields=impressions
but I'm not quite sure where that would fit in a cURL get query, should it look like this ?
https://graph.facebook.com/v2.5/CAMPAIN_ID/insights?fields=impressions%?access_token=TOKEN
I have tried to build the following google script but I'm not sure it's getting anywhere, any help ?
var myClientID = '';
var myClientSecret = '';
var myAccessToken = 'MY_TOKEN';
var graphURL = 'https://graph.facebook.com/v2.5/';
function getPageLikes(campaign_id) {
var searchParams = '?fields=impressions%2Cunique_clicks%2Creach';
var campaignID = MY_CAMPAIGN_ID;
var fullURL = graphURL + campaignID + '/insights/' + searchParams + '&access_token=' + myAccessToken;
var fetchResult = UrlFetchApp.fetch(fullURL);
var campaign = JSON.parse(fetchResult);
var likes = campaign.data[0];
return campaign_data;
}
Thank you
Yes. If you like CURL, you should play with https://developers.facebook.com/tools/explorer/
Then you can formulate something like:
https://graph.facebook.com/v2.9/[campaign_id]/insights?fields=impressions%2Creach&access_token=[token]
But if you want an easier life, I would recommend you to use one of the SDK. There is one for
PHP: https://github.com/facebook/facebook-php-ads-sdk
Python: https://github.com/facebook/facebook-python-ads-sdk
Java: https://github.com/facebook/facebook-java-ads-sdk
And we also have a tool to guide you generate code in the Getting Started session in https://developers.facebook.com/apps/[app_id]/marketing-api/
Basically you can pick metrics and the wizard will generate a working code for you. (It is only generating Java code for now)
Also if you don't really wants to code, you may try the new product we just released called Facebook Ads Manager for Excel. It allows you to download Insights data into Excel directly. More info:
https://www.facebook.com/business/m/facebook-ads-manager-for-excel
I am interested in creating a google app script that on run would login into a specific website (third-party) and complete certain functions within the website (pressing buttons/copying text).
After browsing the stackoverflow and other forums I have created a script that allows me to login into my website (source1 source2).
However, I am having difficulties staying logged in and managing the data.
//The current code is just testing if I can get data from within the website.
//The results are displayed in a google app.
function doGet() {
var app = UiApp.createApplication();
app.add(app.createLabel(display_basic_data()));
return app;
}
//logins into website and displays data
function display_basic_data() {
var data;
var url = "http://www.website.bla/users/sign_in";
var payload = {"user[username]":"usr","user[password]":"ps"};
var opt ={"method":"post","payload":payload, "followRedirects" : false};
var response = UrlFetchApp.fetch(url,opt);
data = response;
return data;
}
Currently, the data returned from display_basic_data() is
"<html><body>You are being redirected.</body></html>".
If I try to change my script so that "followRedirects" is true, the data is equivalent to the HTML of the login page.
I understand I have to play around with cookies in order to 'stay' logged in but I have no idea what to do as the examples online provided to be fruitless for me.
Any help would be much appreciated!!!
You may want to do something like this:
var cookie = response.getAllHeaders()['Set-Cookie'];
//maybe parse cookies here, depends on what cookie is
var headers = {'Cookie':cookie};
var opt2 = {"headers":headers};
var pagedata = UrlFetchApp.fetch("http://www.website.bla/home",opt2);
Google Analytics is not understanding a URL Percent-encoding so I can track multiple domains between my "source" domain and my "destination" domain. I'm using Google Tag Manager + the new Universal Analytics.
Is there a macro or rule in google tag manager that I can create to help Google Analytics detect these two URL Percent-encoding as %2526 for & and %253d for = appropriately? If so, is there any support that could be provided with this issue I'm experiencing?
Here is an example URL (not real):
http://subdomain.example.com/adfs/ls/?wa=wsignin1.0&wtrealm=https%3a%2f%2fsub.domain.com%2fwebsite%2f&wctx=rm%3d0%26id%3dpassive%26ru%3d%252fwebsite%252fsite%252fexample%252f%253fstuff%253dtypeofuser%2526_ga%253d1.244536837.1471787898.1397850931&wct=2014-04-18T20%3a14%3a54Z
As you can see close to the tail end of URL contains my _ga cookie that originated from my "source" domain and is getting passed to my "destination" domain. This is a good thing, however GA is not able to read it, because of the URL Percent-encoding shown below:
%2526_ga%253d1.244536837.1471787898.1397850931
%2526 is a URL encode for &
%253d is a URL encode for =
Since google analytics is not able to translate the URL Percent-encoding %2526 and %253d it writes a brand new cookie instead when I look at my cookies when I debug using firebug > cookies tab.
The solution I found that solves this problem is to append the cookie to the URL again on page load so so the cookie can be read by google analytics.
The regex for .match can be customized with your URLs that you need to filter.
var gacookie = window.location.search.match('_ga%253d(.+)&wct=');
var url = window.location.href;
if (url.indexOf('_ga') > -1) {
url += '&_ga=' + gacookie[1]
parent.location.hash = url
var hash = location.hash.replace('#', gacookie[1]);
if(hash != '') {
location.hash = '&_ga=' + gacookie[1];
}
}
I am trying to Post and get a cookie. I am a newbie and this is a learning project for me. My impression is that if you use 'set-cookie' one should be able to see an additional 'set-cookie' in the .toSource. (I am trying to accomplish this on Google Apps Site if that makes a difference.) Am I missing something? Here is my code:
function setGetCookies() {
var payload = {'set-cookie' : 'test'};
var opt2 = {'headers':payload, "method":"post"};
UrlFetchApp.fetch("https://sites.google.com/a/example.com/blacksmith", opt2);
var response = UrlFetchApp.fetch("https://sites.google.com/a/example.com/blacksmith")
var openId = response.getAllHeaders().toSource();
Logger.log(openId)
var AllHeaders = response.getAllHeaders();
for (var prop in AllHeaders) {
if (prop.toLowerCase() == "set-cookie") {
// if there's only one cookie, convert it into an array:
var myArray = [];
if ( Array.isArray(AllHeaders[prop]) ) {
myArray=AllHeaders[prop];
} else {
myArray[0]=AllHeaders[prop];
}
// now process the cookies
myArray.forEach(function(cookie) {
Logger.log(cookie);
});
break;
}
}
}
Thanks in advance! I referenced this to develop the code: Cookie handling in Google Apps Script - How to send cookies in header?
Open to any advice.
When you aren't logged in Google Sites won't set any cookies in the response. UrlFetchApp doesn't pass along your Google cookies, so it will behave as if you are logged out.
First the cookie you want to send whose name is 'test' does not have a value. You should send 'test=somevalue'.
Second I am wondering if you are trying to send the cookie to the googlesite server and ask it to reply with the same cookie you previously sent... ?
I am thinking you are trying to act as a HTTP server beside you are a HTTP client.
As a HTTP client your role is only to send back any cookies that the HTTP server have previously sent to you (respecting the domain, expiration... params).