Creating reusable function in Google AdWords - javascript

I'm currently writing a script in Google AdWords, and I'm managing lots of campaigns that use the same function (which I have had to copy-paste around 90+ times). Whenever I want to edit this function it becomes incredibly hard as I need to manually to through every script individually.
Is there any way of creating a reusable function, store it as a separate script and call it from AdWords like how I can call Logger and DriveApp? I've tried to use the following resources, which has been pretty useless so far:
Standalone Scripts - Google somehow allows you to create scripts but not execute them from within AdWords (at least that's as far as I've managed to come)
Execution API - Gave up after getting constant 'Unauthenticated' errors (even when setting the permissions to 'Anyone, even anonymous')
MCC Scripts - This allows me to update all accounts, but doesn't do what I need
If anyone knows any way of simply storing a function somewhere and calling it within Google AdWords, it would be greatly appreciated.

You can write your function a save it in a Google Drive file and the use it from there:
var scriptFile = getFile(location);
var scriptText = scriptFile.getBlob().getDataAsString();
eval(scriptText);

Related

How to use Firebase in all documents?

My firebase is setup and initialized in my index.html, but then I can only use it there. Is there any way to use it on all documents?
It is set up in my index page, and then I use the data base and JQuery on a separate JS file to manipulate things also in that index page. I am doing similar things on another HTML page but when I call a function, it doesn't do anything/can't get the data. I thought I should initialize the database on that page too (within script tags of course, just copy/paste the code to initialize), and that also doesn't do anything. I also tried initializing it within the JS code as well, which seems to be the most intuitive thing to do, and that doesn't work either.
I initialize the database with the following:
var firebaseConfig = {...}
firebase.initializeApp(firebaseConfig);
var db = firebase.firestore();
Then just below, I can call a function in the JS and it uses the database information. But when I call a function in another HTML doc, it won't use the database.
I have initialized it in the JS file itself before, and that got me what I wanted but I can't do it again for some reason.
Once you navigate from one page to the next, you browser pretty much forgets anything from the previous page. This means that you will need to include Firebase into any page where you use it.
Firebase is often used in so-called single-page applications, which are implemented as a single real page, but emulate many application screens within that page. That way they only have to include/load Firebase once, but can still show multiple screens to the user. If you're interested in this approach, I recommend looking into some common single-page application frameworks, such as Angular, React, and Vue.

Reference external script in JavaScript Azure Function code

Goal
I would like a specific JavaScript file to be referenced by multiple JavaScript Azure Functions that are managed by different users in different Azure subscriptions. All the subscriptions are within an enterprise subscription. I would also like these multiple users to be able to alter this single-sourced file.
Proposed solution
My best guess on how to accomplish this is to store the file in a public GitHub repository and have each Function reference it.
Problem
Referencing an external script (in my case, the script hosted in GitHub) in standard ways (as I understand them) does not seem to work when implemented in a JavaScript Azure Function.
Code
Structure:
var abc = require('<external reference>');
Sample external JS file:
https://github.com/<username>/<repo>/blob/master/file.js
Failed attempts
There seem to be many outdated methods documented on SO (e.g., rawgit) but I am looking at the answers last edited in 2018.
https://github.com/<username>/<repo>/blob/master/file.js
https://raw.githubusercontent.com/<username>/<repo>/master/file.js
http://cdn.jsdelivr.net/gh/<username>/<repo>/file.js
Alternatives
I know that in the case that multiple Functions within a single Azure Function (and therefore within the same Azure subscription) can reference a centrally managed file by:
Placing it in a Shared folder, adding "watchDirectories": [ "Shared" ] to host.json, and referencing it with require('../Shared/file.js')
Referencing code in one Function from another (e.g., in Function-2 I can use require('../Function-1/file.js'))
However, unless I misunderstand, neither meets the requirements that the Functions can reside in different Functions in different Azure subscriptions.
I am new to JS. Thank you for any guidance. There may be other ways to satisfy these requirements that I am not thinking of.
You won't be able to require the js files in the typical sense from inside a function, as require uses the filesystem to resolve files, which wouldn't really exist inside of a function. If the js you want to run is available on github you could download the source from github. And use the JS Function constructor to run the create a function you can call. Very roughly it might look something like:
let myfunc;
fetchCodeToRun().then(codeString=>myfunc=new Function(codeString)); //untested
Please note that I'm not specifically too familiar with Azure, and I only have a tiny amount of experience with serverless infrastructure so it's very possible that there's a better way of accomplishing this.
Function constructor docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function

Including several Maps from GoogleMaps API without callback

I've been wondering about a way to include a random number of maps in a web page. I'm currently working with GWT and embedding my javascript code into a JSNI function. I have an array of maps but due to my architecture, every map does know nothing about the others. My problem is that I have to include the gmaps script this way (asynchronously):
var script = $doc.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&initializeMaps";
$doc.body.appendChild(script);
The issue here is that I don't know the ids of the maps when loading the script, so I cannot load all of them within the 'initializeMaps' function.
I would like to know whether it is possible to load each map separately instead of depending on the 'initializeMaps' callback, where as I have said, I don't know the maps ids yet.
After a few days I've figured out about how to do it. I thought that loading the Gmaps script at first with no callback wasn't correct and I still don't know, althought I did it.
I included:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
within the head of my document and then I initialized every map with the same function to which I provided map ids as input. As you can see the parameter "initializeMaps" which I was using as callback is not placed in the GMaps url anymore.
The problem is that I have to carry that included script in some places where I don't need it. I don't know whether this is a right approach to the use of GoogleMaps API, but it has been impossible to do it for me without this workaround.

What is the reason behind the use of Javascript in Wikitude SDK for Android?

I've been comparing several SDKs in order to add some location-based AR in one of my Android Apps. So far I've really liked the overall simplicity of Wikitude, however there are things like them asking you to put some of the logic of the AR part in javascript files that confuse me.
What are you supposed to do with the javascript, what role does it serves in the overall flow between an Android Activity and the AR?
Also, as a side question, what sense is there to make out of this line in the Android documentation of Wikitude:
architectView.callJavascript(newData('" + poiDataAsJson +")')
at http://developer.wikitude.com/documentation/android. I assume there is a typo or mistake somewhere as newData is not defined anywhere in the Android code.
Thank you!
In order to enable cross platform augmented reality experiences, web technologies were chosen. The application flow is usually that the ArchtiectView is initialised and displayed by the native application. An AR experience is loaded which consists of an html file that executes javascript functions to create the objects in AR.
callJavascript method is used natively to communicate with the loaded AR exprience. It executes the passed javascript in the context of the loaded AR experience. Therefore it can be used to pass POI data as json to a function declared in the Javascript part.
There is an error in the posted java code
architectView.callJavascript(newData('" + poiDataAsJson +")')
should include 2 more double quotes
architectView.callJavascript("newData('" + poiDataAsJson +")'")
This executes the function newData declared globally in javascript and passes 1 parameter, the json encoded poi data.
Disclaimer: I work for Wikitude

Google Website Optimizer and user-defined variables

I'm trying to merge my Google Website Optimizer A/B testing with Google Analytics (in order to see how page variations affect stats like bounce rate, time on site, etc.). Eric Vasilik, who I believe works at GWO, recommends a technique for doing this that involves setting a user-defined GA variable that is dependent on what page variation the user has been shown. The description of his technique is here: http://www.gwotricks.com/2009/02/poor-mans-gwoanalytics-integration.html
Erik suggests calling the utmx() function on the "original" and "variation" pages in order to grab which page the user has been shown. But when I try doing this, it only works on the "original" page. This is because utmx() is undefined if the GWO control script has not been called, and that script only goes on the "original" page.
Am I missing something here? Since Erik works for GWO I'm sure he's right and I'm misunderstanding the technique, but I can't figure out what I'm doing wrong.
There is something missing here, and that is that the GwoTricks article you reference really only works for multi-variate GWO experiments. There is a way to make this work for GWO A/B experiments. I've updated the article to describe how one does this. But, briefly, the code which sets the user defined variable needs to be moved up from just before the tracking script to be between the two scripts which make up the A/B Control Script. This way, the user defined variable can be set before any redirection takes place so that the tracking script on the alternative pages will have the user defined variable set properly.

Categories

Resources